Skip to content

Commit 9aa84f3

Browse files
committed
Track type name on error before losing that info
1 parent 6274ecd commit 9aa84f3

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/error.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub type Result<T> = std::result::Result<T, Error>;
1515
pub struct Error {
1616
error: anyhow::Error,
1717
status: crate::StatusCode,
18+
type_name: Option<String>,
1819
}
1920

2021
impl Error {
@@ -23,10 +24,11 @@ impl Error {
2324
/// The error type must be threadsafe and 'static, so that the Error will be
2425
/// as well. If the error type does not provide a backtrace, a backtrace will
2526
/// be created here to ensure that a backtrace exists.
26-
pub fn new(status: StatusCode, error: impl Into<anyhow::Error>) -> Self {
27+
pub fn new<E: Into<anyhow::Error>>(status: StatusCode, error: E) -> Self {
2728
Self {
2829
status,
2930
error: error.into(),
31+
type_name: Some(std::any::type_name::<E>().to_string()),
3032
}
3133
}
3234

@@ -38,6 +40,7 @@ impl Error {
3840
Self {
3941
status,
4042
error: anyhow::Error::msg(msg),
43+
type_name: None,
4144
}
4245
}
4346

@@ -102,6 +105,11 @@ impl Error {
102105
{
103106
self.error.downcast_mut::<E>()
104107
}
108+
109+
/// Retrieves a reference to the type name of the error, if available.
110+
pub fn type_name(&self) -> Option<&str> {
111+
self.type_name.as_deref()
112+
}
105113
}
106114

107115
impl Display for Error {

0 commit comments

Comments
 (0)