@@ -16,6 +16,7 @@ pub type Result<T> = std::result::Result<T, Error>;
16
16
pub struct Error {
17
17
error : anyhow:: Error ,
18
18
status : crate :: StatusCode ,
19
+ type_name : Option < String > ,
19
20
}
20
21
21
22
impl Error {
@@ -24,16 +25,18 @@ impl Error {
24
25
/// The error type must be threadsafe and 'static, so that the Error will be
25
26
/// as well. If the error type does not provide a backtrace, a backtrace will
26
27
/// be created here to ensure that a backtrace exists.
27
- pub fn new < S > ( status : S , error : impl Into < anyhow :: Error > ) -> Self
28
+ pub fn new < S , E > ( status : S , error : E ) -> Self
28
29
where
29
30
S : TryInto < StatusCode > ,
30
31
S :: Error : Debug ,
32
+ E : Into < anyhow:: Error > ,
31
33
{
32
34
Self {
33
35
status : status
34
36
. try_into ( )
35
37
. expect ( "Could not convert into a valid `StatusCode`" ) ,
36
38
error : error. into ( ) ,
39
+ type_name : Some ( std:: any:: type_name :: < E > ( ) . to_string ( ) ) ,
37
40
}
38
41
}
39
42
@@ -49,6 +52,7 @@ impl Error {
49
52
. try_into ( )
50
53
. expect ( "Could not convert into a valid `StatusCode`" ) ,
51
54
error : anyhow:: Error :: msg ( msg) ,
55
+ type_name : None ,
52
56
}
53
57
}
54
58
/// Create a new error from a message.
@@ -112,6 +116,11 @@ impl Error {
112
116
{
113
117
self . error . downcast_mut :: < E > ( )
114
118
}
119
+
120
+ /// Retrieves a reference to the type name of the error, if available.
121
+ pub fn type_name ( & self ) -> Option < & str > {
122
+ self . type_name . as_deref ( )
123
+ }
115
124
}
116
125
117
126
impl Display for Error {
0 commit comments