SANDBOX-1282 | refactor: replace "wrapf" calls with stdlib wrap calls#526
Conversation
There is no need to keep the legacy "wrapf" calls to wrap errors anymore, so the idea is to remove them in favor of the wrapping calls of the stdlib. SANDBOX-1282
WalkthroughUpdates error handling across three packages to use Go's native error wrapping ( Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
There was a problem hiding this comment.
🧹 Nitpick comments (3)
pkg/template/nstemplatetiers/nstemplatetier_generator.go (1)
60-186: Remainingerrors.Wrap/errors.Errorfnot converted.Lines 60, 66, 72, and 186 still use
github.com/pkg/errors. To fully complete the stdlib-wrapping refactor in this file (and drop thegithub.com/pkg/errorsimport), convert these as well — e.g.:- return errors.Wrap(err, "unable to init NSTemplateTier generator") + return fmt.Errorf("unable to init NSTemplateTier generator: %w", err) ... - return nil, errors.Errorf("unable to load templates: unknown scope for file '%s'", name) + return nil, fmt.Errorf("unable to load templates: unknown scope for file '%s'", name)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/template/nstemplatetiers/nstemplatetier_generator.go` around lines 60 - 186, The file still uses github.com/pkg/errors in loadTemplatesByTiers (and the surrounding init code) — replace remaining errors.Wrap and errors.Errorf usages with stdlib fmt.Errorf using %w for wrapping (e.g. return fmt.Errorf("unable to init NSTemplateTier generator: %w", err) and return fmt.Errorf("unable to load templates: unknown scope for file '%s'", name) or use %w where wrapping is required), update the error returns in newNSTemplateTierGenerator and loadTemplatesByTiers accordingly, and remove the pkg/errors import; search for errors.Wrap, errors.Errorf and convert them to fmt.Errorf with proper %w wrapping and plain formatting where no wrapping is needed.pkg/client/client.go (1)
238-254: Double-wrapping inApplyand leftovererrors.Wrap.Two observations:
- Line 254 wraps the error returned by
ApplyObject, butApplyObject(line 100) already wraps with the exact same"unable to create resource of kind: %s, version: %s"message. The resulting error chain will contain that prefix twice (e.g.,unable to create resource of kind: X, version: Y: unable to create resource of kind: X, version: Y: <cause>). Consider returning the error unchanged fromApply, or removing the duplicate wrap at one of the two layers. Note: this duplication predates this PR, but the conversion is a good moment to fix it.- Line 238 still uses
errors.Wrapfromgithub.com/pkg/errors. For consistency with the rest of this refactor, convert it tofmt.Errorf("unable to set controller references: %w", err)so thepkg/errorsimport can eventually be dropped.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/client/client.go` around lines 238 - 254, The Apply method is double-wrapping errors from ApplyObject and still uses errors.Wrap elsewhere; fix by returning the error from Apply unchanged (i.e., replace the fmt.Errorf wrap in Apply that references toolchainObject.GetObjectKind().GroupVersionKind() with a plain return false, err) so ApplyObject remains the single author of that context, and also replace any uses of errors.Wrap (the one that wraps "unable to set controller references") with fmt.Errorf("unable to set controller references: %w", err) so pkg/errors can be removed consistently; target the Apply function and the code path that sets controller references (where errors.Wrap is used) to make these changes.pkg/cluster/service.go (1)
68-82: Remainingerrors.Wrapcalls not converted.Lines 68, 82, 108, and 160 still use
github.com/pkg/errors(errors.Wrap/errors.Errorf). If the goal of the PR is to standardize on stdlib wrapping, these sites in the same file should be converted too; otherwise thegithub.com/pkg/errorsimport cannot be removed and the refactor remains inconsistent.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/cluster/service.go` around lines 68 - 82, Several error uses in this file still call github.com/pkg/errors (errors.Wrap / errors.Errorf); update those to use the standard library error wrapping so the pkg/errors import can be removed. Replace errors.Wrap(err, "msg") with fmt.Errorf("msg: %w", err) and replace errors.Errorf("format", args...) with fmt.Errorf("format", args...), targeting the error sites within functions such as addToolchainCluster (and any other functions in this file that call errors.Wrap/errors.Errorf), and ensure the file imports "fmt" and removes the "github.com/pkg/errors" import.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@pkg/client/client.go`:
- Around line 238-254: The Apply method is double-wrapping errors from
ApplyObject and still uses errors.Wrap elsewhere; fix by returning the error
from Apply unchanged (i.e., replace the fmt.Errorf wrap in Apply that references
toolchainObject.GetObjectKind().GroupVersionKind() with a plain return false,
err) so ApplyObject remains the single author of that context, and also replace
any uses of errors.Wrap (the one that wraps "unable to set controller
references") with fmt.Errorf("unable to set controller references: %w", err) so
pkg/errors can be removed consistently; target the Apply function and the code
path that sets controller references (where errors.Wrap is used) to make these
changes.
In `@pkg/cluster/service.go`:
- Around line 68-82: Several error uses in this file still call
github.com/pkg/errors (errors.Wrap / errors.Errorf); update those to use the
standard library error wrapping so the pkg/errors import can be removed. Replace
errors.Wrap(err, "msg") with fmt.Errorf("msg: %w", err) and replace
errors.Errorf("format", args...) with fmt.Errorf("format", args...), targeting
the error sites within functions such as addToolchainCluster (and any other
functions in this file that call errors.Wrap/errors.Errorf), and ensure the file
imports "fmt" and removes the "github.com/pkg/errors" import.
In `@pkg/template/nstemplatetiers/nstemplatetier_generator.go`:
- Around line 60-186: The file still uses github.com/pkg/errors in
loadTemplatesByTiers (and the surrounding init code) — replace remaining
errors.Wrap and errors.Errorf usages with stdlib fmt.Errorf using %w for
wrapping (e.g. return fmt.Errorf("unable to init NSTemplateTier generator: %w",
err) and return fmt.Errorf("unable to load templates: unknown scope for file
'%s'", name) or use %w where wrapping is required), update the error returns in
newNSTemplateTierGenerator and loadTemplatesByTiers accordingly, and remove the
pkg/errors import; search for errors.Wrap, errors.Errorf and convert them to
fmt.Errorf with proper %w wrapping and plain formatting where no wrapping is
needed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro Plus
Run ID: 28d3ec50-859f-49e4-8798-4015aa9a0b4b
📒 Files selected for processing (3)
pkg/client/client.gopkg/cluster/service.gopkg/template/nstemplatetiers/nstemplatetier_generator.go
94789ab
into
codeready-toolchain:master



There is no need to keep the legacy "wrapf" calls to wrap errors anymore, so the idea is to remove them in favor of the wrapping calls of the stdlib.
Related PRs
Jira ticket
[SANDBOX-1282]
Summary by CodeRabbit