Skip to content

Commit

Permalink
Merge pull request eyre-rs#113 from chris-laplante/cpl/suppress_backt…
Browse files Browse the repository at this point in the history
…race

add `suppress_backtrace` method
  • Loading branch information
yaahc authored Aug 4, 2022
2 parents ad68589 + 93fcb6f commit 4a7b4d6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ impl EyreHook {
crate::Handler {
filters: self.filters.clone(),
backtrace,
suppress_backtrace: false,
#[cfg(feature = "capture-spantrace")]
span_trace,
sections: Vec::new(),
Expand Down
17 changes: 10 additions & 7 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,17 @@ impl eyre::EyreHandler for Handler {
}
}

if let Some(backtrace) = self.backtrace.as_ref() {
let fmted_bt = self.format_backtrace(backtrace);
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
)?;
write!(
indented(&mut separated.ready())
.with_format(Format::Uniform { indentation: " " }),
"{}",
fmted_bt
)?;
}
}

let f = separated.ready();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ mod writers;
pub struct Handler {
filters: Arc<[Box<config::FilterCallback>]>,
backtrace: Option<Backtrace>,
suppress_backtrace: bool,
#[cfg(feature = "capture-spantrace")]
span_trace: Option<SpanTrace>,
sections: Vec<HelpInfo>,
Expand Down
13 changes: 13 additions & 0 deletions src/section/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<crate::Handler>() {
handler.suppress_backtrace = suppress;
}

self
}
}

impl<T, E> Section for Result<T, E>
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions src/section/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4a7b4d6

Please sign in to comment.