From: Ian Jackson Date: Sat, 7 Aug 2021 15:52:05 +0000 (+0100) Subject: eyre: wip backtrace, c&p from example X-Git-Tag: hippotat/1.0.0~287 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=1546303f5347ad95b63e6fa5d5b516bb0668e837;p=hippotat.git eyre: wip backtrace, c&p from example Signed-off-by: Ian Jackson --- diff --git a/Cargo.lock b/Cargo.lock index 98af578..3ffac8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -429,6 +429,7 @@ dependencies = [ "hippotat-macros", "hyper", "hyper-tls", + "indenter", "ipnet", "itertools", "lazy-regex", diff --git a/Cargo.toml b/Cargo.toml index 0649f6e..7c71354 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,8 +47,8 @@ void = "1" # Not in sid: extend = "1" # no deps not in sid -eyre = "0.6" # deps not in sid: -# indenter # no deps not in sid +eyre = "0.6" # deps not in sid: indenter +indenter = "0.3" # no deps not in sid fehler = "1" # no deps (other than fehler-macros, obvs) lazy-regex = "2" # no deps not in sid cervine = "0.0" # no (non-dev)-deps not in sid diff --git a/src/reporter.rs b/src/reporter.rs index 29bf1cc..926b094 100644 --- a/src/reporter.rs +++ b/src/reporter.rs @@ -1,4 +1,4 @@ -// Copyright 2021 Ian Jackson and contributors to Hippotat +// Copyright 2021 Ian Jackson, yaahc and contributors to Hippotat and Eyre // SPDX-License-Identifier: GPL-3.0-or-later // There is NO WARRANTY. @@ -92,10 +92,10 @@ impl<'r> Reporter<'r> { write!(m, " ({}ok)", self.successes)?; self.successes = 0; } - write!(m, ": {}", e)?; + write!(m, ": {:?}", e)?; Ok::<_,fmt::Error>(m) })().unwrap(); - warn!(target:"hippotat", "{:?}", m); + warn!(target:"hippotat", "{}", m); self.last_report = Some(Report { when: now, ok: Err(()) }); None }, @@ -104,7 +104,10 @@ impl<'r> Reporter<'r> { } use backtrace::Backtrace; +use eyre::Chain; +use indenter::indented; +#[derive(Debug)] struct EyreDedupHandler { backtrace: Backtrace, } @@ -114,7 +117,28 @@ type EyreDynError<'r> = &'r (dyn std::error::Error + 'static); impl eyre::EyreHandler for EyreDedupHandler { #[throws(fmt::Error)] fn debug(&self, error: EyreDynError, f: &mut fmt::Formatter) { - Debug::fmt(error, f)?; + if f.alternate() { + return core::fmt::Debug::fmt(error, f)?; + } + + write!(f, "{}", error)?; + + if let Some(cause) = error.source() { + write!(f, "\n\nCaused by:")?; + let multiple = cause.source().is_some(); + + for (n, error) in Chain::new(cause).enumerate() { + writeln!(f)?; + if multiple { + write!(indented(f).ind(n), "{}", error)?; + } else { + write!(indented(f), "{}", error)?; + } + } + } + + let backtrace = &self.backtrace; + write!(f, "\n\nStack backtrace:\n{:?}", backtrace)?; } #[throws(fmt::Error)] fn display(&self, error: EyreDynError, f: &mut fmt::Formatter) {