Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Error Display (std::error::Error::fmt_error) #3459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ijackson wants to merge 11 commits into rust-lang:master
base: master
Choose a base branch
Loading
from ijackson:error

Conversation

@ijackson
Copy link

@ijackson ijackson commented Jul 19, 2023

Aloso and teor2345 reacted with thumbs up emoji clarfonthey reacted with eyes emoji
Copy link
Member

I haven't fully read the RFC, but wanted to drop some additional prior art. snafu::report recognizes that many errors in the ecosystem basically implement Display as write!(f, "My unique error text: {source}"), which looks terrible when trying to format a nested error as you end up with:

1: C: B: A
2: B: A
3: A

Report post-processes those error messages to remove the duplicated content, producing:

1: C
2: B
3: A
Enet4 and Aloso reacted with thumbs up emoji

Copy link
Member

cc @yaahc, whose eyre crate has an extensive reporting mechanism.

yaahc reacted with eyes emoji

Copy link

I like the concept but I don't like much the limitation of current Display and Debug trait, you can't really do any sort of user configuration, like for example, carry information like if NO_COLOR is set in the environnement (you need to use static) thus I got no idea how we could solve this.

The fmt argument I mostly ignore all its features and just use it cause it has the write item.

I asked something very similar to tracing crate tokio-rs/tracing#2226

@ehuss ehuss added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Jul 20, 2023
Copy link

oxalica commented Jul 28, 2023
edited
Loading

@shepmaster

I haven't fully read the RFC, but wanted to drop some additional prior art. snafu::report recognizes that many errors in the ecosystem basically implement Display as write!(f, "My unique error text: {source}"), which looks terrible when trying to format a nested error as you end up with:

1: C: B: A
2: B: A
3: A

The problem is that both documentations of std::error and/or std::error::Error do not mention the preferred output style for errors wrapping other errors. And it should be clarified.

I personally does not use any error reporting libraries, and use simple eprintln!("{myerror}");, which calls fmt::Display on the outmost error, resulting in unhelpful generic "failed to open file" without detail reasons. So it forces me for implements write!(f, "failed to open file: {}", self.inner) for these MyError.

In this RFC:

A new error_fmt method on std::error::Error, so that we can distinguish:

  • Requests to just display an error for human consumption (Display)
  • The internal implementation of printing a particular error, excluding its sources (error_fmt).

I doubt that if this can be achieved by the alternative flag {:#}. Like, print itself for {}, print the whole chain for {:#}. So that terminal users and pretty print libraries can choose their own formatting. This would be a documentation change, not a std change, and require users to follow.

Copy link
Author

Thanks to everyone for the review and comments. I've updated the RFC. Some more detailed reponses:

@shepmaster

I haven't fully read the RFC, but wanted to drop some additional prior art. snafu::report [addresses this problem]

Thanks for the reference, indeed. I wasn't aware of that. I think that this textual deduplication approach has been independently invented (at least) twice shows it's a viable strategy.

@Stargateur

I like the concept but I don't like much the limitation of current Display and Debug trait, you can't really do any sort of user configuration, like for example, carry information like if NO_COLOR is set in the environnement (you need to use static) thus I got no idea how we could solve this.

Indeed, this RFC won't eliminate the desire to do that, and it doesn't help with it very much. For example, if you want coloured error messages, you'll still need to write a reporter that prints errors in colour, and arrange to call it everywhere you print an error.

But it does make it marginally easier: at least, with this RFC, you will (eventually) be able to reliably format the individual errors without their sources being mixed in.

@oxalica

I doubt that if this can be achieved by the alternative flag {:#}

I have added a new section about this to Alternatives. As you will see from my text, I think there are serious downsides to that approach.

@joshtriplett

cc @yaahc, whose eyre crate has an extensive reporting mechanism.

Yes. One way of looking at eyre is provides errors with a better Display, by wrapping them up. I have added a reference. eyre makes printing sources depend on {} vs {:?}, so I mention that too.

Copy link
Contributor

GH isn't highlighting the code, could you change the fences to ```rust?

ijackson reacted with thumbs up emoji

Copy link
Contributor

kpreid commented Jul 31, 2023

I am glad to hear of a plan to make it easy and automatic to show source chains, thus relieving the tension between showing sources in Display (DWIM for printing and logging; makes it harder for error handling code to neglect to show the sources) and not showing sources in Display (enables clean structured chain printing). I don't like the result that nearly every error will automatically have the same default Display implementation, and that that implementation will have to choose some chain formatting (the standard library mostly does not put choices of syntax into Display, except for things like integers being base 10). But, an application can always introduce its own top-level error type with choice of Display, which is already common practice with e.g. anyhow.

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

Reviewers

2 more reviewers

@Stargateur Stargateur Stargateur left review comments

@deltragon deltragon deltragon left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /