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

Remove all uses of implicit error propagation #401

Merged
merged 1 commit into from
Dec 25, 2024
Merged

Conversation

chenxiaolong
Copy link
Owner

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:

Caused by:
    0: Failed to patch payload: payload.bin
    1: Failed to patch boot images: boot, init_boot, vendor_boot
    2: Boot image error
    3: I/O error
    4: failed to fill whole buffer

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:

Caused by:
    0: Failed to patch payload: payload.bin
    1: Failed to patch boot images: boot, init_boot, vendor_boot
    2: Failed to load boot image: init_boot
    3: Failed to read boot image data: Boot::V3::ramdisk
    4: failed to fill whole buffer

Changes:

  • Remove all uses of #[from] from thiserror-derived error types.
  • Errors during parsing and serialization of RSA private keys, RSA public keys, and X509 certificates now include the file path.
  • Removed unnecessary uses of BufReader and BufWriter when reading and writing RSA private keys, RSA public keys, and X509 certificates, since they need to be fully read into memory anyway.
  • Use ReadFixedSizeExt instead of read_exact() where possible.
  • Use &'static str instead of String in error fields where all possible values are known at compile-time to avoid unnecessary heap allocation.
  • Use ok_or() instead of ok_or_else() to construct errors when the error variant uses known data and does not require heap allocation.
  • Using DebugString instead of String in error variants that store a preformatted debug string.

While working on this commit, an unrelated bug was found and fixed:

  • Fix vendor v4 boot images that were truncated within the padding following the bootconfig section being treated as valid.

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:

    Caused by:
        0: Failed to patch payload: payload.bin
        1: Failed to patch boot images: boot, init_boot, vendor_boot
        2: Boot image error
        3: I/O error
        4: failed to fill whole buffer

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:

    Caused by:
        0: Failed to patch payload: payload.bin
        1: Failed to patch boot images: boot, init_boot, vendor_boot
        2: Failed to load boot image: init_boot
        3: Failed to read boot image data: Boot::V3::ramdisk
        4: failed to fill whole buffer

Changes:

* Remove all uses of #[from] from thiserror-derived error types.
* Errors during parsing and serialization of RSA private keys, RSA
  public keys, and X509 certificates now include the file path.
* Removed unnecessary uses of BufReader and BufWriter when reading and
  writing RSA private keys, RSA public keys, and X509 certificates,
  since they need to be fully read into memory anyway.
* Use ReadFixedSizeExt instead of read_exact() where possible.
* Use &'static str instead of String in error fields where all possible
  values are known at compile-time to avoid unnecessary heap allocation.
* Use ok_or() instead of ok_or_else() to construct errors when the error
  variant uses known data and does not require heap allocation.
* Using DebugString instead of String in error variants that store a
  preformatted debug string.

While working on this commit, an unrelated bug was found and fixed:

* Fix vendor v4 boot images that were truncated within the padding
  following the bootconfig section being treated as valid.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
@chenxiaolong chenxiaolong self-assigned this Dec 25, 2024
chenxiaolong added a commit that referenced this pull request Dec 25, 2024
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
@chenxiaolong chenxiaolong merged commit a2fb807 into master Dec 25, 2024
5 checks passed
@chenxiaolong chenxiaolong deleted the errors branch December 25, 2024 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant