mirror of
https://github.com/imjasonh/pkgerrors-analyzer
synced 2026-07-07 00:32:34 +00:00
No description
|
|
||
|---|---|---|
| cmd/pkgerrors-analyzer | ||
| testdata/src | ||
| analyzer.go | ||
| analyzer_test.go | ||
| fixes.go | ||
| go.mod | ||
| go.sum | ||
| imports.go | ||
| LICENSE | ||
| README.md | ||
pkgerrors-analyzer
github.com/pkg/errors was archived in December 2021, after the Go stdlib added built-in support for wrapping, unwrapping and joining errors. Even so, there are still 300k+ imports of pkg/errors in the wild.
This is a Go anaylzer that finds uses of pkg/errors and replaces them with errors and fmt.Errorf instead.
Replacements
| Before | After |
|---|---|
errors.New(msg) |
errors.New(msg) (import swap) |
errors.Errorf(fmt, args...) |
fmt.Errorf(fmt, args...) |
errors.Wrap(err, "msg") |
fmt.Errorf("msg: %w", err) |
errors.Wrapf(err, "fmt", args...) |
fmt.Errorf("fmt: %w", args..., err) |
errors.WithMessage(err, "msg") |
fmt.Errorf("msg: %w", err) |
errors.WithMessagef(err, "fmt", args...) |
fmt.Errorf("fmt: %w", args..., err) |
errors.WithStack(err) |
reported, no auto-fix (stack trace lost) |
errors.Cause(err) == target |
errors.Is(err, target) |
errors.Cause(err) != target |
!errors.Is(err, target) |
errors.Cause(err) (other) |
reported, no auto-fix |
errors.Unwrap(err) |
errors.Unwrap(err) (import swap) |
errors.Is(err, target) |
errors.Is(err, target) (import swap) |
errors.As(err, target) |
errors.As(err, target) (import swap) |
Some methods in pkg/errors are not automatically replaceable, and will just report a diagnostic instead of automatically fixing with -fix.
Behavioral differences
These replacements are not always perfectly equivalent. Review changes before committing.
- Stack traces:
pkg/errors.New,Errorf,Wrap, andWrapfall capture a stack trace. The stdlib replacements do not. If you depend on stack traces in error output (e.g.%+vformatting), those will be lost. - Nil wrapping:
pkg/errors.Wrap(nil, "msg")returnsnil.fmt.Errorf("msg: %w", nil)returns a non-nil error. Code that checksif err != nilafter wrapping may behave differently. - Cause vs Is:
errors.Causeunwraps to the root error only.errors.Ismatches anywhere in the chain. This is usually more correct, but could match unexpectedly if the same sentinel appears as both a wrapper and a root cause.
Usage
pkgerrors-analyzer ./... # report diagnostics
pkgerrors-analyzer -fix ./... # apply fixes