Repository navigation
feat: support %w verb in Errorf for error wrapping - #254
AdamMagued wants to merge 3 commits into
Conversation
Signed-off-by: AdamMagued <adamismailmageud@gmail.com>
|
Why not just make fundamental implement Unwrap? |
|
There are a couple of architectural considerations around
That said, if you prefer to keep pkg/errors focused strictly on single-error wrapping and would rather unify single-wrap |
| err := fmt.Errorf(format, args...) | ||
| if u, ok := err.(interface{ Unwrap() error }); ok { | ||
| return &wrapError{ | ||
| msg: err.Error(), | ||
| err: u.Unwrap(), | ||
| stack: callers(), | ||
| } | ||
| } | ||
| if u, ok := err.(interface{ Unwrap() []error }); ok { | ||
| return &wrapErrors{ | ||
| msg: err.Error(), | ||
| errs: u.Unwrap(), | ||
| stack: callers(), | ||
| } | ||
| } |
There was a problem hiding this comment.
😐 A type switch would make more sense here.
There was a problem hiding this comment.
Updated in eae2cc5: replaced the separate type assertions with switch u := err.(type)
| type wrapError struct { | ||
| msg string | ||
| err error | ||
| *stack | ||
| } |
There was a problem hiding this comment.
Why recreate &withMessage{ cause: &fundamental{ … } }?
There was a problem hiding this comment.
Updated in eae2cc5: wrapError now embeds *fundamental to reuse Error(), Format(), and StackTrace() without duplicating them. withMessage.Error() prepends w.msg + ": ", which alters fmt.Errorf formatting when %w appears mid-string or alone
There was a problem hiding this comment.
OK, now I suppose I’ll ask the question: why recreate WithStack(fmt.Errorf(format, args...))?
There was a problem hiding this comment.
WithStack(fmt.Errorf(format, args...)) produces a three-layer chain: withStack -> fmt.wrapError -> cause. Callers would need a double Unwrap to reach the cause. More critically, withStack.Format delegates to the inner error's Format method, and fmt.wrapError has no custom Format, so %+v output loses the full stack frame rendering that fundamental.Format provides.
By embedding *fundamental directly, wrapError preserves the exact Error(), Format(), and StackTrace() behavior that Errorf has always returned. The chain stays flat at two levels (wrapError -> cause), and Unwrap returns the cause directly. The only addition is the Unwrap() error method so the wrapped error participates in errors.Is/errors.As.
There was a problem hiding this comment.
How many layers of unwrapping are necessary is irrelevant. errors.Is and errors.As walks through the unwraps indefinitely. That’s the whole point of the Unwrap design: we don’t have to implement this stuff ourselves.
More critically,
withStack.Formatdelegates to the inner error'sFormatmethod, andfmt.wrapErrorhas no customFormat, so%+voutput loses the full stack frame rendering thatfundamental.Formatprovides.
What? https://github.com/pkg/errors/blob/master/errors.go#L165-L179
func (w *withStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", w.Cause())
w.stack.Format(s, verb) // ← right here!!! We print the stacktrace with verb `%+v`.
return
}
fallthrough
case 's':
io.WriteString(s, w.Error())
case 'q':
fmt.Fprintf(s, "%q", w.Error())
}
}Having fewer layers to the error chain isn’t necessarily a positive. Handing more features off to the standard library, and having less self-built NIH code is a solid win.
There was a problem hiding this comment.
Good point on withStack.Format and errors.Is/errors.As walking Unwrap() chains. Updated in f92838e: removed wrapError and wrapErrors, and Errorf now returns &withStack{err, callers()} when fmt.Errorf wraps a non-nil error (keeping *fundamental otherwise so Cause still preserves StackTrace()).
| if u, ok := err.(interface{ Unwrap() error }); ok { | ||
| return &wrapError{ | ||
| msg: err.Error(), | ||
| err: u.Unwrap(), |
There was a problem hiding this comment.
We should probably check to make sure that u.Unwrap is non-nil… otherwise, we should just return a fundamental.
There was a problem hiding this comment.
Updated in eae2cc5: Errorf now checks u.Unwrap() != nil and returns *fundamental when nil
| if u, ok := err.(interface{ Unwrap() []error }); ok { | ||
| return &wrapErrors{ | ||
| msg: err.Error(), | ||
| errs: u.Unwrap(), |
There was a problem hiding this comment.
Similarly, if this returns a length 1 unwrap, we could just return a wrapError and a length 0 unwrap, we could just return a fundamental.
There was a problem hiding this comment.
Updated in eae2cc5: 0-length slices (and 1-length with nil) return *fundamental, and 1-length non-nil returns *wrapError
|
|
||
| type wrapErrors struct { | ||
| msg string | ||
| errs []error |
There was a problem hiding this comment.
Why recreate &withMessage{ cause: errors.Join( &fundamental { … }, … ) }?
There was a problem hiding this comment.
Updated in eae2cc5: wrapErrors now embeds *fundamental as well. errors.Join requires Go 1.20+ (this package targets Go 1.13+) and joins messages with newlines rather than preserving fmt.Errorf formatting
…ndamental Signed-off-by: AdamMagued <adamismailmageud@gmail.com>
Signed-off-by: AdamMagued <adamismailmageud@gmail.com>
Fixes #244
Solution
Verified with container sandbox test suite.