From 5aa7c40f380225ac55daef342cd75682949807c5 Mon Sep 17 00:00:00 2001 From: Chris Laplante Date: Fri, 15 Jul 2022 17:41:16 -0400 Subject: [PATCH 1/2] add suppress_backtrace --- src/config.rs | 1 + src/handler.rs | 18 ++++++++++-------- src/lib.rs | 1 + src/section/help.rs | 13 +++++++++++++ src/section/mod.rs | 6 ++++++ 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0a12444..0c83b0b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1030,6 +1030,7 @@ impl EyreHook { crate::Handler { filters: self.filters.clone(), backtrace, + suppress_backtrace: false, #[cfg(feature = "capture-spantrace")] span_trace, sections: Vec::new(), diff --git a/src/handler.rs b/src/handler.rs index 3f2a1a1..7c2e25b 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -111,14 +111,16 @@ impl eyre::EyreHandler for Handler { } } - if let Some(backtrace) = self.backtrace.as_ref() { - let fmted_bt = self.format_backtrace(backtrace); - - write!( - indented(&mut separated.ready()).with_format(Format::Uniform { indentation: " " }), - "{}", - fmted_bt - )?; + if !self.suppress_backtrace { + if let Some(backtrace) = self.backtrace.as_ref() { + let fmted_bt = self.format_backtrace(backtrace); + + write!( + indented(&mut separated.ready()).with_format(Format::Uniform { indentation: " " }), + "{}", + fmted_bt + )?; + } } let f = separated.ready(); diff --git a/src/lib.rs b/src/lib.rs index 2acf68a..aee5e68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -400,6 +400,7 @@ mod writers; pub struct Handler { filters: Arc<[Box]>, backtrace: Option, + suppress_backtrace: bool, #[cfg(feature = "capture-spantrace")] span_trace: Option, sections: Vec, diff --git a/src/section/help.rs b/src/section/help.rs index e4ed1fe..a03cf3e 100644 --- a/src/section/help.rs +++ b/src/section/help.rs @@ -142,6 +142,14 @@ impl Section for Report { self } + + fn suppress_backtrace(mut self, suppress: bool) -> Self::Return { + if let Some(handler) = self.handler_mut().downcast_mut::() { + handler.suppress_backtrace = suppress; + } + + self + } } impl Section for Result @@ -234,6 +242,11 @@ where self.map_err(|error| error.into()) .map_err(|report| report.error(error())) } + + fn suppress_backtrace(self, suppress: bool) -> Self::Return { + self.map_err(|error| error.into()) + .map_err(|report| report.suppress_backtrace(suppress)) + } } pub(crate) enum HelpInfo { diff --git a/src/section/mod.rs b/src/section/mod.rs index 5b1b77d..4455890 100644 --- a/src/section/mod.rs +++ b/src/section/mod.rs @@ -315,6 +315,12 @@ pub trait Section: crate::private::Sealed { where D: Display + Send + Sync + 'static, F: FnOnce() -> D; + + /// Whether to suppress printing of collected backtrace (if any). + /// + /// Useful for reporting "unexceptional" errors for which a backtrace + /// isn't really necessary. + fn suppress_backtrace(self, suppress: bool) -> Self::Return; } /// Trait for printing a panic error message for the given PanicInfo From 2821e2b4b89308bd9d22f7f9654deeb6c268072b Mon Sep 17 00:00:00 2001 From: Chris Laplante Date: Fri, 29 Jul 2022 22:00:17 -0400 Subject: [PATCH 2/2] cargo fmt --- src/handler.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/handler.rs b/src/handler.rs index 1dd67d5..ab7fdc4 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -118,7 +118,8 @@ impl eyre::EyreHandler for Handler { let fmted_bt = self.format_backtrace(backtrace); write!( - indented(&mut separated.ready()).with_format(Format::Uniform { indentation: " " }), + indented(&mut separated.ready()) + .with_format(Format::Uniform { indentation: " " }), "{}", fmted_bt )?;