@@ -161,6 +161,28 @@ impl Error {
161
161
pub fn type_name ( & self ) -> Option < & str > {
162
162
self . type_name . as_deref ( )
163
163
}
164
+
165
+ /// Converts anything which implements `Display` into an `http_types::Error`.
166
+ ///
167
+ /// This is handy for errors which are not `Send + Sync + 'static` because `std::error::Error` requires `Display`.
168
+ /// Note that any assiciated context not included in the `Display` output will be lost,
169
+ /// and so this may be lossy for some types which implement `std::error::Error`.
170
+ ///
171
+ /// **Note: Prefer `error.into()` via `From<Into<anyhow::Error>>` when possible!**
172
+ pub fn from_display < D : Display > ( error : D ) -> Self {
173
+ anyhow:: Error :: msg ( error. to_string ( ) ) . into ( )
174
+ }
175
+
176
+ /// Converts anything which implements `Debug` into an `http_types::Error`.
177
+ ///
178
+ /// This is handy for errors which are not `Send + Sync + 'static` because `std::error::Error` requires `Debug`.
179
+ /// Note that any assiciated context not included in the `Debug` output will be lost,
180
+ /// and so this may be lossy for some types which implement `std::error::Error`.
181
+ ///
182
+ /// **Note: Prefer `error.into()` via `From<Into<anyhow::Error>>` when possible!**
183
+ pub fn from_debug < D : Debug > ( error : D ) -> Self {
184
+ anyhow:: Error :: msg ( format ! ( "{:?}" , error) ) . into ( )
185
+ }
164
186
}
165
187
166
188
impl Display for Error {
@@ -180,6 +202,7 @@ impl<E: Into<anyhow::Error>> From<E> for Error {
180
202
Self :: new ( StatusCode :: InternalServerError , error)
181
203
}
182
204
}
205
+
183
206
impl AsRef < dyn StdError + Send + Sync > for Error {
184
207
fn as_ref ( & self ) -> & ( dyn StdError + Send + Sync + ' static ) {
185
208
self . error . as_ref ( )
0 commit comments