Remove all uses of implicit error propagation #401
Merged
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.
Implicit error propagation was originally used because it was convenient and made it easy to just bubble up errors via the
?
operator without thinking. However, there have been too many situations where this resulted in error messages that were completely useless in troubleshooting the problem.I/O error
, even with a specific reason attached, is useless where there are potentially hundreds of operations where I/O can fail.I no longer think implicit error propagation is a good idea, so this commit removes every single use of
#[from]
in every custom error type. There are now many more error variants, allowing more context to be attached to the underlying errors.Previously, it was easy to encounter error messages like:
This is a terrible error message because it doesn't mention which of the 3 boot images failed to parse, nor does it mention during which I/O operation it encountered EOF. With this commit, this sort of information is now included. For example, if the boot image happened to be truncated in the middle of the ramdisk, the error message would now be:
Changes:
#[from]
from thiserror-derived error types.BufReader
andBufWriter
when reading and writing RSA private keys, RSA public keys, and X509 certificates, since they need to be fully read into memory anyway.ReadFixedSizeExt
instead ofread_exact()
where possible.&'static str
instead ofString
in error fields where all possible values are known at compile-time to avoid unnecessary heap allocation.ok_or()
instead ofok_or_else()
to construct errors when the error variant uses known data and does not require heap allocation.DebugString
instead ofString
in error variants that store a preformatted debug string.While working on this commit, an unrelated bug was found and fixed: