Skip to content

SANDBOX-1282 | refactor: replace "wrapf" calls with stdlib wrap calls#1257

Merged
MikelAlejoBR merged 3 commits intocodeready-toolchain:masterfrom
MikelAlejoBR:SANDBOX-1282-refactor-replace-wrapf-errorf-ho
Apr 29, 2026
Merged

SANDBOX-1282 | refactor: replace "wrapf" calls with stdlib wrap calls#1257
MikelAlejoBR merged 3 commits intocodeready-toolchain:masterfrom
MikelAlejoBR:SANDBOX-1282-refactor-replace-wrapf-errorf-ho

Conversation

@MikelAlejoBR
Copy link
Copy Markdown
Contributor

@MikelAlejoBR MikelAlejoBR commented Apr 22, 2026

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

  • Refactor
    • Improved error handling across the app to preserve underlying error context, leading to clearer and more informative error messages during user signup approval, template processing, notification generation, and startup operations.

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
@openshift-ci openshift-ci Bot requested review from rajivnathan and xcoulon April 22, 2026 21:10
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 9fac7a91-bd92-4a65-a86f-70abd9aa4f26

📥 Commits

Reviewing files that changed from the base of the PR and between cb5400b and 60d6a05.

📒 Files selected for processing (2)
  • pkg/templates/notificationtemplates/notification_generator.go
  • pkg/templates/usertiers/usertier_generator.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/templates/notificationtemplates/notification_generator.go

Walkthrough

Replaced usages of github.com/pkg/errors wrapping with Go's standard fmt.Errorf and %w across four files, adjusting error construction and messages without changing control flow or exported declarations.

Changes

Cohort / File(s) Summary
Command entrypoint
cmd/main.go
Replaced errors.Wrapf with fmt.Errorf(...: %w, err) in addMemberClusters error handling.
UserSignup controller
controllers/usersignup/approval.go
Replaced errors.Wrapf with fmt.Errorf(...: %w, err) for three error cases in getClusterIfApproved.
Notification templates
pkg/templates/notificationtemplates/notification_generator.go
Changed error construction for template-path/filename validation: one case now returns errors.New with context, another uses fmt.Errorf(...: %w, err) instead of errors.Wrapf.
UserTier templates
pkg/templates/usertiers/usertier_generator.go
Replaced errors.Wrapf with fmt.Errorf(...: %w, err) across template loading/generation and resource creation flows; adjusted unknown-scope error formatting.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: refactoring error wrapping from legacy 'wrapf' calls to Go's standard library 'fmt.Errorf' with '%w', which is clearly reflected across all modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Linked repositories: Couldn't analyze codeready-toolchain/toolchain-common - clone failed: Clone operation failed: Cloning into '/home/jailuser/git'...
warning: templates not found in /usr/share/git-core/templates
remote: Internal Server Error
fatal: unable to access 'https://github.com/codeready-toolchain/toolchain-common.git/': The requested URL returned error: 500


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
pkg/templates/notificationtemplates/notification_generator.go (1)

63-63: Avoid wrapping a freshly-created static-string error with %w.

fmt.Errorf("unable to load templates: %w", errors.New("path must contain directory and file")) wraps a synthetic error whose only content is a literal string — unwrapping yields nothing a caller can meaningfully match on (no sentinel, no typed error). This is just a two-part message dressed up as a wrapped error, and it still keeps the github.com/pkg/errors dependency alive in this file.

Consider collapsing into a single fmt.Errorf (no %w needed) and dropping the pkg/errors import:

♻️ Proposed change
-			return nil, fmt.Errorf("unable to load templates: %w", errors.New("path must contain directory and file"))
+			return nil, fmt.Errorf("unable to load templates: path must contain directory and file")
-			return nil, fmt.Errorf("unable to load templates: %w", errors.New("must contain notification.html and subject.txt"))
+			return nil, fmt.Errorf("unable to load templates: must contain notification.html and subject.txt")

Note: errors.Wrap at Line 38 still uses pkg/errors; migrating it would allow removing the import entirely, consistent with this PR's goal.

Also applies to: 78-78

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/templates/notificationtemplates/notification_generator.go` at line 63,
Replace the unnecessary wrapped static error in the fmt.Errorf call (currently
using "%w" with errors.New("path must contain directory and file")) with a
single fmt.Errorf message (e.g., fmt.Errorf("unable to load templates: path must
contain directory and file")), and remove the now-unused github.com/pkg/errors
import; also migrate any remaining uses of pkg/errors (notably the errors.Wrap
call earlier in this file) to the stdlib fmt.Errorf or errors package so the
pkg/errors dependency can be dropped entirely.
pkg/templates/usertiers/usertier_generator.go (1)

36-42: Migration is incomplete in this file.

Line 146 is correctly migrated, but several pkg/errors calls remain and keep the dependency alive:

  • Line 36: errors.Wrap(err, "unable to create UserTier generator")fmt.Errorf("unable to create UserTier generator: %w", err)
  • Line 42: errors.Wrap(err, "unable to create UserTiers")fmt.Errorf("unable to create UserTiers: %w", err)
  • Line 155: errors.Errorf("unable to load templates: unknown scope for file '%s'", name)fmt.Errorf(...) (no wrapped error needed here)

Given the PR’s stated intent to remove legacy pkg/errors wrapping, consider migrating these too so the import can be dropped from this file.

Also applies to: 146-146, 155-155

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/templates/usertiers/usertier_generator.go` around lines 36 - 42, Replace
remaining pkg/errors usage in this file: change errors.Wrap(err, "unable to
create UserTier generator") and errors.Wrap(err, "unable to create UserTiers")
to fmt.Errorf("unable to create UserTier generator: %w", err) and
fmt.Errorf("unable to create UserTiers: %w", err) respectively, and replace
errors.Errorf("unable to load templates: unknown scope for file '%s'", name)
with fmt.Errorf("unable to load templates: unknown scope for file %q", name);
update the imports by removing "github.com/pkg/errors" and adding "fmt" (or
using the existing fmt import) so the package no longer depends on pkg/errors;
ensure these changes are applied in the functions that call createUserTiers and
the template-loading code paths (refer to createUserTiers and the
template-loading error string).
🤖 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/templates/notificationtemplates/notification_generator.go`:
- Line 63: Replace the unnecessary wrapped static error in the fmt.Errorf call
(currently using "%w" with errors.New("path must contain directory and file"))
with a single fmt.Errorf message (e.g., fmt.Errorf("unable to load templates:
path must contain directory and file")), and remove the now-unused
github.com/pkg/errors import; also migrate any remaining uses of pkg/errors
(notably the errors.Wrap call earlier in this file) to the stdlib fmt.Errorf or
errors package so the pkg/errors dependency can be dropped entirely.

In `@pkg/templates/usertiers/usertier_generator.go`:
- Around line 36-42: Replace remaining pkg/errors usage in this file: change
errors.Wrap(err, "unable to create UserTier generator") and errors.Wrap(err,
"unable to create UserTiers") to fmt.Errorf("unable to create UserTier
generator: %w", err) and fmt.Errorf("unable to create UserTiers: %w", err)
respectively, and replace errors.Errorf("unable to load templates: unknown scope
for file '%s'", name) with fmt.Errorf("unable to load templates: unknown scope
for file %q", name); update the imports by removing "github.com/pkg/errors" and
adding "fmt" (or using the existing fmt import) so the package no longer depends
on pkg/errors; ensure these changes are applied in the functions that call
createUserTiers and the template-loading code paths (refer to createUserTiers
and the template-loading error string).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 405ec1c8-1a41-4aca-8ec6-32ec0613bba2

📥 Commits

Reviewing files that changed from the base of the PR and between 1bbb3cf and cb5400b.

📒 Files selected for processing (4)
  • cmd/main.go
  • controllers/usersignup/approval.go
  • pkg/templates/notificationtemplates/notification_generator.go
  • pkg/templates/usertiers/usertier_generator.go

Copy link
Copy Markdown
Contributor

@metlos metlos left a comment

Choose a reason for hiding this comment

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

Yay for using the stdlib :) Just minor comments below that don't block the merge IMHO.

segments := strings.Split(path, "/")
if len(segments) != 5 {
return nil, errors.Wrapf(errors.New("path must contain directory and file"), "unable to load templates")
return nil, fmt.Errorf("unable to load templates: %w", errors.New("path must contain directory and file"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agreed with coderabbit here. errors.New doesn't make sense here. I'd just use fmt.Errorf with just the single string. Alternatively, you can use errors.New with that string or even a global private error var with this error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I went with a simple errors.New because we don't need to errors.As or errors.Is it

notificationTemplates[directoryName] = template
default:
return nil, errors.Wrapf(errors.New("must contain notification.html and subject.txt"), "unable to load templates")
return nil, fmt.Errorf("unable to load templates: %w", errors.New("must contain notification.html and subject.txt"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, done!

@openshift-ci
Copy link
Copy Markdown

openshift-ci Bot commented Apr 23, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexeykazakov, metlos, mfrancisc, MikelAlejoBR

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [alexeykazakov,metlos,mfrancisc]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sonarqubecloud
Copy link
Copy Markdown

@MikelAlejoBR MikelAlejoBR merged commit 251ea71 into codeready-toolchain:master Apr 29, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants