8000 explicitly handle errors when wrapping them by thaJeztah · Pull Request #5851 · docker/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

explicitly handle errors when wrapping them #5851

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

Merged
merged 1 commit into from
Feb 26, 2025

Conversation

thaJeztah
Copy link
Member

The errors.Wrap and errors.Wrapf functions gracefully handle nil-errors. This allows them to be used unconditionally regardless if an error was produced.

While this can be convenient, it can also be err-prone, as replacing these with stdlib errors means they unconditionally produce an error.

This patch replaces code uses of errors.Wrap to be gated by a check for nil-errors to future-proof our code.

- What I did

- How I did it

- How to verify it

- Human readable description for the release notes

- A picture of a cute animal (not mandatory but encouraged)

The errors.Wrap and errors.Wrapf functions gracefully handle nil-errors.
This allows them to be used unconditionally regardless if an error
was produced.

While this can be convenient, it can also be err-prone, as replacing
these w
8000
ith stdlib errors means they unconditionally produce an error.

This patch replaces code uses of errors.Wrap to be gated by a check
for nil-errors to future-proof our code.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code labels Feb 20, 2025
@codecov-commenter
Copy link
codecov-commenter commented Feb 20, 2025

Codecov Report

Attention: Patch coverage is 22.72727% with 17 lines in your changes missing coverage. Please review.

Project coverage is 58.74%. Comparing base (f9ced58) to head (da4b627).
Report is 28 commits behind head on master.

❌ Your patch status has failed because the patch coverage (22.72%) is below the target coverage (50.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5851      +/-   ##
==========================================
- Coverage   58.81%   58.74%   -0.07%     
==========================================
  Files         349      349              
  Lines       29556    29570      +14     
==========================================
- Hits        17382    17371      -11     
- Misses      11189    11212      +23     
- Partials      985      987       +2     

@@ -67,7 +67,10 @@ func recursiveInterpolate(value any, path Path, opts Options) (any, error) {
return newValue, nil
}
casted, err := caster(newValue)
return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type"))
if err != nil {
return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure if caster would still return a value, so I left that one as-is, and just return it (as it did before); it should probably be nil or "something else" though.

@@ -121,7 +121,10 @@ func (c *client) PutManifest(ctx context.Context, ref reference.Named, manifest
}

dgst, err := manifestService.Put(ctx, manifest, opts...)
return dgst, errors.Wrapf(err, "failed to put manifest %s", ref)
if err != nil {
return dgst, errors.Wrapf(err, "failed to put manifest %s", ref)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, is dgst set on error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, honestly, same situation as my other comment; we should probably return a nil or "zero" value here, because results should (almost) never be consumed if there's an error, but this was also a case where I would have to check all possible implementations; manifestService.Get is an interface, so now the fun starts to check which one we actually use, and how that one behaves 🙈

So I was taking the "safe" side and kept it as-is, leaving that for a future exercise 😅

Screenshot 2025-02-21 at 10 33 31

Copy link
Collaborator
@laurazard laurazard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (I really prefer this, I always find myself double-checking the other usages to make sure they're returning nil if the input is nil).

@thaJeztah
Copy link
Member Author
thaJeztah commented Feb 21, 2025

always find myself double-checking the other usages to make sure they're returning nil if the input is nil).

💯 for return values as well (I made some changes similar in the client some time ago); I like seeing return nil, err or return EmptyStruct{}, err, because if you read that, you know at a glance nobody can depend on any (possible "garbage") returned, whereas return <some value that could be garbage>, err means "some code could depend on this, even if there's an error!"

(which could be valid for some sentinel errors, but those should be an exception, so having them stand out as "this is different" is better as well!)

@thaJeztah
Copy link
Member Author

FWIW; we're considering a patch release after the weekend, and want to skip the "cherry-pick" dance and instead fast-forward the release branches.

This change should probably do no harm but doesn't fix any bugs or otherwise, so we can decide to keep this one for "later" to keep the diff small (either way would be fine with me though); cc @neersighted @vvoland

@thaJeztah
Copy link
Member Author

Bringing this one in, as we did v28.0.1

@thaJeztah thaJeztah merged commit b414752 into docker:master Feb 26, 2025
98 checks passed
@thaJeztah thaJeztah deleted the err_handle_explicit branch February 26, 2025 15:23
@thaJeztah thaJeztah added this to the 28.0.2 milestone Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/refactor PR's that refactor, or clean-up code status/2-code-review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0