Skip to content

Commit 644fdf8

Browse files
committed
Track type name on error before losing that info
1 parent a9885f1 commit 644fdf8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/error.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub type Result<T> = std::result::Result<T, Error>;
1616
pub struct Error {
1717
error: anyhow::Error,
1818
status: crate::StatusCode,
19+
type_name: Option<String>,
1920
}
2021

2122
impl Error {
@@ -24,16 +25,18 @@ impl Error {
2425
/// The error type must be threadsafe and 'static, so that the Error will be
2526
/// as well. If the error type does not provide a backtrace, a backtrace will
2627
/// 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
2829
where
2930
S: TryInto<StatusCode>,
3031
S::Error: Debug,
32+
E: Into<anyhow::Error>,
3133
{
3234
Self {
3335
status: status
3436
.try_into()
3537
.expect("Could not convert into a valid `StatusCode`"),
3638
error: error.into(),
39+
type_name: Some(std::any::type_name::<E>().to_string()),
3740
}
3841
}
3942

@@ -49,6 +52,7 @@ impl Error {
4952
.try_into()
5053
.expect("Could not convert into a valid `StatusCode`"),
5154
error: anyhow::Error::msg(msg),
55+
type_name: None,
5256
}
5357
}
5458
/// Create a new error from a message.
@@ -112,6 +116,11 @@ impl Error {
112116
{
113117
self.error.downcast_mut::<E>()
114118
}
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+
}
115124
}
116125

117126
impl Display for Error {

0 commit comments

Comments
 (0)