You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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 ofstd::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
, ormiette
. 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 addsdisplay_chain()
method to the error trait that returns an implementor ofstd::fmt::Display
that will include source errors information (code link)Example code
Bad
Good
Notes
The lint should cover any conversion of the value implementing
std::error::Error
tostd::fmt::Display
. It should detect the calls of.to_string()
, see through conversions of the value todyn Display
or when it's passed asimpl Display
.The text was updated successfully, but these errors were encountered: