diff --git a/src/error.rs b/src/error.rs index a6eae7ea..179adaa9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,6 +16,7 @@ pub type Result = std::result::Result; pub struct Error { error: anyhow::Error, status: crate::StatusCode, + type_name: Option<&'static str>, } impl Error { @@ -24,16 +25,18 @@ impl Error { /// The error type must be threadsafe and 'static, so that the Error will be /// as well. If the error type does not provide a backtrace, a backtrace will /// be created here to ensure that a backtrace exists. - pub fn new(status: S, error: impl Into) -> Self + pub fn new(status: S, error: E) -> Self where S: TryInto, S::Error: Debug, + E: Into, { Self { status: status .try_into() .expect("Could not convert into a valid `StatusCode`"), error: error.into(), + type_name: Some(std::any::type_name::()), } } @@ -49,6 +52,7 @@ impl Error { .try_into() .expect("Could not convert into a valid `StatusCode`"), error: anyhow::Error::msg(msg), + type_name: None, } } /// Create a new error from a message. @@ -112,6 +116,11 @@ impl Error { { self.error.downcast_mut::() } + + /// Retrieves a reference to the type name of the error, if available. + pub fn type_name(&self) -> Option<&str> { + self.type_name.as_deref() + } } impl Display for Error {