fix: use %w instead of %s for error wrapping in fmt.Errorf calls#1222
Open
AdeshDeshmukh wants to merge 1 commit into
Open
fix: use %w instead of %s for error wrapping in fmt.Errorf calls#1222AdeshDeshmukh wants to merge 1 commit into
AdeshDeshmukh wants to merge 1 commit into
Conversation
| availPort, err := findAvailablePort() | ||
| if err != nil { | ||
| return fmt.Errorf("invalid arguments: %s", err) | ||
| return fmt.Errorf("invalid arguments: %w", err) |
…ss the codebase Changes %s to %w in 11 fmt.Errorf calls where the argument is an error type, enabling proper error chain unwrapping via errors.Is()/errors.As(). This is a Go best practice that ensures callers can inspect the wrapped error rather than receiving a stringified version. Includes cleanup of a stray trailing \n in one error format string. Affected packages: pkg/lib/filesystem/unpack/ (4 occurrences in core.go, skill.go, util.go) pkg/lib/repo/local/ (2 occurrences in migration.go) pkg/cmd/remove/ (2 occurrences in remove.go) pkg/cmd/tag/ (1 occurrence in tag.go) pkg/cmd/kitimport/ (1 occurrence in util.go) pkg/cmd/dev/ (1 occurrence in opts.go) Signed-off-by: Adesh Deshmukh <adeshkd123@gmail.com>
6e98e92 to
4c60582
Compare
Author
|
Agreed that message was misleading. Updated in the latest push. |
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.
Description
This PR fixes 11 instances across 8 files where
fmt.Errorfwas using%sto format an error argument instead of%w. While the error messages read just fine either way, using%ssilently breaks the error chain — callers usingerrors.Is()orerrors.As()cannot unwrap through these errors to inspect the underlying cause.This is a Go best practice that the rest of the codebase already follows correctly (many adjacent lines use
%wproperly). These were likely copy-paste oversights.What was changed
Every fix is the same pattern:
%s→%wfor the error argument in afmt.Errorfcall.pkg/lib/filesystem/unpackcore.go,skill.go,util.gogetStoreForRef,GetManifest, andNewLocalRepoerrorspkg/lib/repo/localmigration.goos.RemoveAllerrors during storage migrationpkg/cmd/removeremove.goremoveModelRefandoras.Resolveerrorspkg/cmd/tagtag.gooras.Resolveerrorpkg/cmd/kitimportutil.goos.WriteFileerrorpkg/cmd/devopts.gofindAvailablePorterrorAdditional cleanup
In
pkg/lib/filesystem/unpack/util.go, the format string also had a stray trailing\n("failed to read local storage: %s\n"), which was removed as part of the fix. Trailing newlines are unusual in Go error messages and can cause formatting issues in logs.Verification
go vet ./...— ✅ passesgo build ./...— ✅ compilesgo test ./...— ✅ 63 tests pass across 6 affected packagesWhy this matters
Consider a caller trying to inspect the error from
removeModel():Before this fix, the
%sverb stringifies the underlying error, soerrors.Is()never matches — even though the codebase explicitly checkserr == errdef.ErrNotFoundin the same functions. After the fix, the error chain is preserved and unwrapping works as intended.Checklist
go vetreports no issuesgo buildsucceeds