From aa757a5be8a88eccc88ad5f0785ee35ec7d9ecac Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 29 Apr 2018 14:21:50 -0700 Subject: [PATCH] Allow to access Error's backtrace object --- CHANGES.md | 2 ++ src/error.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 9d3e0a10a..a771ce37a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,8 @@ * Add Content-Disposition to NamedFile #204 +* Allow to access Error's backtrace object + ## 0.5.6 (2018-04-24) diff --git a/src/error.rs b/src/error.rs index 3004adef3..37dc3d89f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -45,6 +45,16 @@ impl Error { pub fn cause(&self) -> &ResponseError { self.cause.as_ref() } + + /// Returns a reference to the Backtrace carried by this error, if it + /// carries one. + pub fn backtrace(&self) -> Option<&Backtrace> { + if let Some(bt) = self.cause.backtrace() { + Some(bt) + } else { + self.backtrace.as_ref() + } + } } /// Error that can be converted to `HttpResponse` @@ -793,6 +803,13 @@ mod tests { assert_eq!(format!("{}", e.cause().unwrap()), desc); } + #[test] + fn test_backtrace() { + let orig = ErrorBadRequest("err"); + let e: Error = orig.into(); + assert!(e.backtrace().is_some()); + } + #[test] fn test_error_cause() { let orig = io::Error::new(io::ErrorKind::Other, "other");