Skip to content

Migrate from pkg/errors to standard library and enable depguard linter#1002

Merged
theyoprst merged 1 commit intodevfrom
feat/migrate-from-pkg-errors-to-stdlib
Jun 16, 2025
Merged

Migrate from pkg/errors to standard library and enable depguard linter#1002
theyoprst merged 1 commit intodevfrom
feat/migrate-from-pkg-errors-to-stdlib

Conversation

@theyoprst
Copy link
Collaborator

@theyoprst theyoprst commented Jun 15, 2025

Summary

Replace all usage of github.com/pkg/errors with Go's standard library error handling using fmt.Errorf() and the %w verb, and enable depguard linter to prevent future usage.

Part of #680. Closes #1001.

Changes Made

  • Replaced all errors.Wrap(err, "message") with fmt.Errorf("message: %w", err)
  • Removed github.com/pkg/errors imports from source files
  • Added depguard linter configuration to prevent future pkg/errors usage
  • Preserved standard library errors.New() usage where appropriate

Benefits

  • Dependencies: Reduced external dependency footprint
  • Future-proof: Linter enforcement prevents regression to deprecated patterns
  • Compatibility: Better integration with Go 1.13+ error handling (errors.Is, errors.As)
  • Standards: Aligns with modern Go development best practices

Breaking Changes

None. All error wrapping semantics are preserved through the %w verb.

@theyoprst theyoprst added this to the Tech debt milestone Jun 15, 2025
@theyoprst theyoprst requested a review from rdjjke as a code owner June 15, 2025 16:36
@theyoprst theyoprst added the enhancement New feature or request label Jun 15, 2025
@theyoprst theyoprst requested a review from Uburro as a code owner June 15, 2025 16:36
@theyoprst theyoprst added the go Pull requests that update Go code label Jun 15, 2025
@theyoprst theyoprst requested a review from Copilot June 15, 2025 18:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR migrates error wrapping from pkg/errors to the Go standard library via fmt.Errorf() with the %w verb and enforces this pattern with a new depguard linter rule.

  • Replaces pkg/errors.Wrap with fmt.Errorf(...) in numerous controller files
  • Removes pkg/errors import and updates go.mod accordingly
  • Adds a depguard linter configuration to prevent reintroduction of pkg/errors

Reviewed Changes

Copilot reviewed 39 out of 39 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/controller/reconciler/k8s_job.go Error wrapping updated using fmt.Errorf
internal/controller/reconciler/k8s_daemonset.go Error wrapping updated using fmt.Errorf
internal/controller/reconciler/k8s_cronjob.go Error wrapping updated using fmt.Errorf
internal/controller/reconciler/k8s_configmap.go Error wrapping updated using fmt.Errorf
internal/controller/reconciler/grant.go Error wrapping updated; note potential formatting inconsistency
internal/controller/reconciler/deployment.go Error wrapping updated using fmt.Errorf
internal/controller/reconciler/apparmorprofile.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/worker.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/sconfigcontroller.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/rest.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/reconcile.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/populate_job.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/login.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/exporter.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/controller.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/common.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/benchmark.go Error wrapping updated using fmt.Errorf
internal/controller/clustercontroller/accounting.go Error wrapping updated using fmt.Errorf
go.mod pkg/errors dependency removed and reordering updated
.golangci.yaml depguard linter rule added to disallow pkg/errors

Replace all usage of github.com/pkg/errors with Go's standard library
error handling using fmt.Errorf() and the %w verb.

- Replace ~240+ errors.Wrap() calls with fmt.Errorf() using %w verb
- Remove github.com/pkg/errors imports from source files
- Add depguard linter configuration to prevent future pkg/errors usage
- Clean up import ordering and fix error message formatting consistency

Benefits:
- Reduced external dependency footprint
- Future-proof with linter enforcement against regression
- Better integration with Go 1.13+ error handling (errors.Is, errors.As)
- Aligns with modern Go development best practices
@theyoprst theyoprst force-pushed the feat/migrate-from-pkg-errors-to-stdlib branch from cd7a8c7 to 717aa11 Compare June 15, 2025 18:18
@theyoprst
Copy link
Collaborator Author

Thanks for the review! ✅ Fixed the formatting inconsistency.

Changes made:

  • Removed extra spaces before colons in error messages across 4 files
  • Now consistently using "message: %w" format instead of "message : %w"
  • All error messages now follow the same formatting pattern

The fix has been included in the squashed commit 717aa11.

@theyoprst theyoprst merged commit 875c725 into dev Jun 16, 2025
4 checks passed
@asteny asteny deleted the feat/migrate-from-pkg-errors-to-stdlib branch June 23, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable depguard linter and migrate from pkg/errors to standard library

3 participants