[Draft] Make build.rs check error logging and chaining#4127
Draft
[Draft] Make build.rs check error logging and chaining#4127
build.rs check error logging and chaining#4127Conversation
Collaborator
|
This should have had a discussed issue before we start writing code.
Can you give concrete examples of the difference in what is printed? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a mechanism to ensure errors are logged and chained in the "correct" way.
The following two common issues are addressed:
anyhow!("my message - {err}")warn!("{err}")instead ofwarn!("{}", flatten_error(err)).Implementation Details
Similar to the locktick checks, these checks are performed at build type using the
build.rsscript. The code walks the syntax tree of every file to looks for the use of variables namederr,e, orerror. It then ensures the error is properly formatted or chained.It will then print the offending location and line as an error, which can look something like the following:
There are some cases where you want to concatenate error messages, e.g., sometimes we prefix a context such as
[Gateway]. For these cases, the PR adds macros and helper functions to do this explicitly, such asbail_concat!andprefix_error. The latter changes the top-level error message, without discarding the error chain.Finally, I also updated the locktick checks to use the same syntax tree approach. It is much cleaner and allows for more detailed error messages, such as the following:
Notes
anyhow::Errorhas a built-in way to flatten errors. It uses the alternative formatting, i.e.,{err:#}. However, that mechanisms uses colons as separators, and it does not work withthiserrortypes. See Display cause chain when alternate format is used dtolnay/thiserror#98 for more information.