fix: use %w instead of %v/%s in fmt.Errorf to preserve error chains - #3730
Merged
sebrandon1 merged 1 commit intoJun 17, 2026
Conversation
This was referenced Jun 16, 2026
Merged
sebrandon1
force-pushed
the
fix/error-wrapping-preserve-chains
branch
2 times, most recently
from
June 16, 2026 17:39
820f06b to
c6b0938
Compare
Collaborator
|
from change #3730: |
Replace %v and %s with %w in fmt.Errorf calls where the argument is an error value. Using %v or %s converts the error to a plain string, breaking the error chain and preventing callers from using errors.Is() or errors.As() to inspect wrapped errors. This change covers 199 call sites across 50 files. Lines where the format verb corresponds to a non-error argument (strings, slices, status codes) or where err may be nil due to compound conditions (err != nil || stderr != "") are intentionally left as %v. Test assertions using assert.Equal(t, errors.New(...), err) were updated to assert.EqualError(t, err, msg) since %w produces *fmt.wrapError instead of *errors.errorString.
sebrandon1
force-pushed
the
fix/error-wrapping-preserve-chains
branch
from
June 17, 2026 17:59
c6b0938 to
45a25b0
Compare
Collaborator
|
from change #3730: |
sebrandon1
merged commit Jun 17, 2026
54b878b
into
redhat-best-practices-for-k8s:main
43 checks passed
Collaborator
|
from change #3730: |
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.
Summary
%vand%swith%win 199fmt.Errorfcall sites across 50 files where the argument is anerrorvalueerrors.Is()anderrors.As()to inspect wrapped errors (Go blog: Working with Errors in Go 1.13)assert.Equal(t, errors.New(...))) to useassert.EqualErrorinsteadHow is this different than prior attempts?
This repo has had two prior error-handling PRs that addressed a different problem:
return err— added context where there was nonereturn err— same, larger sweepfmt.Errorfcalls that already had context but used%v/%sinstead of%wThe prior PRs added wrapping where none existed (bare
return err). This PR fixes wrapping that was already there but used the wrong format verb, silently converting errors to strings and breakingerrors.Is()/errors.As()chains.Details
Using
%vor%sinfmt.Errorfconverts the error to a plain string, silently breaking the error chain. This preventserrors.Is()anderrors.As()from matching wrapped errors, making error handling unreliable.Lines where the format verb corresponds to a non-error argument (strings, slices, status codes) or where
errmay be nil due to compound conditions (err != nil || stderr != "") are intentionally left as%v.Related to
%werror wrapping in tls-scanner (merged)%werror wrapping in commatrix (merged)%werror wrapping in crc (merged)Test plan
make buildpassesmake testpasses (all packages)golangci-lint run ./...reports 0 issues%v/%scalls are legitimate non-error uses