Skip to content

Commit

Permalink
Display the whole error stack when formatting errors
Browse files Browse the repository at this point in the history
According to the format suggested in tokio-rs#1347 (comment)

Fixes tokio-rs#1347
  • Loading branch information
nightkr committed Jul 9, 2021
1 parent 57885b1 commit b7bea69
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 22 additions & 1 deletion tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,10 @@ impl<'a> field::Visit for DefaultVisitor<'a> {

fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) {
if let Some(source) = value.source() {
self.record_debug(field, &format_args!("{}, {}: {}", value, field, source))
self.record_debug(
field,
&format_args!("{}, {}.sources: {}", value, field, ErrorSourceList(source)),
)
} else {
self.record_debug(field, &format_args!("{}", value))
}
Expand Down Expand Up @@ -806,6 +809,24 @@ impl<'a> fmt::Debug for DefaultVisitor<'a> {
}
}

/// Renders an error into a list of sources, *including* the error
struct ErrorSourceList<'a>(&'a (dyn std::error::Error + 'static));

impl<'a> Display for ErrorSourceList<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_char('[')?;
let mut curr = Some(self.0);
while let Some(curr_err) = curr {
f.write_fmt(format_args!("{}", curr_err))?;
curr = curr_err.source();
if curr.is_some() {
f.write_str(", ")?;
}
}
f.write_char(']')
}
}

struct FmtCtx<'a, S, N> {
ctx: &'a FmtContext<'a, S, N>,
span: Option<&'a span::Id>,
Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,12 @@ impl<'a> field::Visit for PrettyVisitor<'a> {
self.record_debug(
field,
&format_args!(
"{}, {}{}.source{}: {}",
"{}, {}{}.sources{}: {}",
value,
bold.prefix(),
field,
bold.infix(self.style),
source,
ErrorSourceList(source),
),
)
} else {
Expand Down

0 comments on commit b7bea69

Please sign in to comment.