Skip to content
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

Lint bare_display_error #39

Open
Veetaha opened this issue Jul 26, 2023 · 0 comments
Open

Lint bare_display_error #39

Veetaha opened this issue Jul 26, 2023 · 0 comments
Labels
A-user-story Area: A user story or a related issue

Comments

@Veetaha
Copy link

Veetaha commented Jul 26, 2023

Lint explanation

Catch code that formats the error using its std::fmt::Display implementation. The problem with such code is that by convention that I use errors should contain only the error message describing the context where they happened. The source error information should not be included in the error message when it is displayed. So.. if you just display an implementor of std::error::Error, you will get only the error message from the top-level error of the errors chain.

Instead, to display the error developers must use a specialized method that will iterate over the chain of source errors and print them nicely just like it is done in anyhow, or miette. Unfortunatelly, there isn't a standard and opinionated method to make this a standard lint, so I am willing to implement it on the project-specific basis. For example, in my repo I have an extension trait that adds display_chain() method to the error trait that returns an implementor of std::fmt::Display that will include source errors information (code link)

Example code

Bad

println!("{err}");

Good

use my_display_errors_chain_impl::ErrorExt;

println!("{}", err.display_chain());

Notes

The lint should cover any conversion of the value implementing std::error::Error to std::fmt::Display. It should detect the calls of .to_string(), see through conversions of the value to dyn Display or when it's passed as impl Display.

@Veetaha Veetaha added the A-user-story Area: A user story or a related issue label Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-user-story Area: A user story or a related issue
Projects
None yet
Development

No branches or pull requests

1 participant