From 644fdf8c6be7b716632af43fc2f4cba882f75e91 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Wed, 5 Aug 2020 10:12:01 -0700 Subject: [PATCH 1/2] Track type name on error before losing that info --- src/error.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index a6eae7ea..0f76ce56 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, } 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::().to_string()), } } @@ -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 { From 2c6e3f14a74a14ee8f9f30d7ad14bbeea838724e Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Fri, 7 Aug 2020 13:06:00 -0700 Subject: [PATCH 2/2] Apply suggestions from code review keep the type_name a &'static str Co-authored-by: Yoshua Wuyts --- src/error.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index 0f76ce56..179adaa9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,7 +16,7 @@ pub type Result = std::result::Result; pub struct Error { error: anyhow::Error, status: crate::StatusCode, - type_name: Option, + type_name: Option<&'static str>, } impl Error { @@ -36,7 +36,7 @@ impl Error { .try_into() .expect("Could not convert into a valid `StatusCode`"), error: error.into(), - type_name: Some(std::any::type_name::().to_string()), + type_name: Some(std::any::type_name::()), } }