Skip to content

Conversation

@Frando
Copy link

@Frando Frando commented Oct 6, 2025

Implements a fix to #390:

When printing an error that has the Snafu derive with the alternate display formatter ({err:#}), the list of sources is attached, similar to how anyhow does it.

This is similar to the Report, but without needing to take ownership of the error (which Report requireds) and without spreading the display over multiple lines.

It is especially useful when logging errors while also bubbling them up. We frequently want to log errors, and currently this will not print the source chain, rendering the log mostly irrelevant. With this change, you can do

tracing::warn!("{err:#}")

And get a log like

Get failed: IO failure: pipe is broken

(from the simple example - which I can remove of course if this PR is being considered for merging).

@netlify
Copy link

netlify bot commented Oct 6, 2025

Deploy Preview for shepmaster-snafu failed.

Name Link
🔨 Latest commit 4d7d341
🔍 Latest deploy log https://app.netlify.com/projects/shepmaster-snafu/deploys/68e389c19f4b06000898fda2

Copy link
Owner

@shepmaster shepmaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not expect to accept this PR in the current form:

  1. I do not think that every error type constructed by SNAFU should have this functionality (increasing the amount of code generated).
  2. Putting sources in Display, even through the alternate formatter, counteracts the guidance to not do so.

Instead, I could instead see this being an alternate to Report for the cases where you need a single-line error. For example:

tracing::warn!("{}", ReportOneline::new(err));

Implementation-wise, this should use CleanedErrorText (when the right feature(s) are enabled) to avoid repeating the error text from errors that already don't follow the guidelines.

but without needing to take ownership of the error (which Report requireds)

It does not:

use snafu::prelude::*;

#[derive(Debug, Snafu)]
struct ExampleError;

fn main() {
    let e = ExampleError;
    let r = snafu::Report::from_error(&e);
    println!("{r}");
}

I'd expect a hypothetical ReportOneline to behave similarly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants