Repository navigation
feat: support %w verb in Errorf for error wrapping #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,13 +109,68 @@ func New(message string) error { | |
| // Errorf formats according to a format specifier and returns the string | ||
| // as a value that satisfies error. | ||
| // Errorf also records the stack trace at the point it was called. | ||
| // If the format specifier includes a %w verb with an error operand, | ||
| // the returned error will implement an Unwrap method returning the operand. | ||
| func Errorf(format string, args ...interface{}) error { | ||
| return &fundamental{ | ||
| msg: fmt.Sprintf(format, args...), | ||
| err := fmt.Errorf(format, args...) | ||
| f := &fundamental{ | ||
| msg: err.Error(), | ||
| stack: callers(), | ||
| } | ||
| switch u := err.(type) { | ||
| case interface{ Unwrap() error }: | ||
| if unwrapped := u.Unwrap(); unwrapped != nil { | ||
| return &wrapError{ | ||
| fundamental: f, | ||
| err: unwrapped, | ||
| } | ||
| } | ||
| case interface{ Unwrap() []error }: | ||
| errs := u.Unwrap() | ||
| switch len(errs) { | ||
| case 0: | ||
| case 1: | ||
| if errs[0] != nil { | ||
| return &wrapError{ | ||
| fundamental: f, | ||
| err: errs[0], | ||
| } | ||
| } | ||
| default: | ||
| return &wrapErrors{ | ||
| fundamental: f, | ||
| errs: errs, | ||
| } | ||
| } | ||
| } | ||
| return f | ||
| } | ||
|
|
||
| type wrapError struct { | ||
| *fundamental | ||
| err error | ||
| } | ||
|
|
||
| func (w *wrapError) Cause() error { return w.err } | ||
|
|
||
| // Unwrap provides compatibility for Go 1.13 error chains. | ||
| func (w *wrapError) Unwrap() error { return w.err } | ||
|
|
||
| type wrapErrors struct { | ||
| *fundamental | ||
| errs []error | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why recreate
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in eae2cc5: |
||
| } | ||
|
|
||
| func (w *wrapErrors) Cause() error { | ||
| if len(w.errs) > 0 { | ||
| return w.errs[0] | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Unwrap provides compatibility for Go 1.20 error chains. | ||
| func (w *wrapErrors) Unwrap() []error { return w.errs } | ||
|
|
||
| // fundamental is an error that has a message and a stack, but no caller. | ||
| type fundamental struct { | ||
| msg string | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why recreate
&withMessage{ cause: &fundamental{ … } }?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in eae2cc5:
wrapErrornow embeds*fundamentalto reuseError(),Format(), andStackTrace()without duplicating them.withMessage.Error()prependsw.msg + ": ", which altersfmt.Errorfformatting when%wappears mid-string or aloneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, now I suppose I’ll ask the question: why recreate
WithStack(fmt.Errorf(format, args...))?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WithStack(fmt.Errorf(format, args...))produces a three-layer chain:withStack -> fmt.wrapError -> cause. Callers would need a doubleUnwrapto reach the cause. More critically,withStack.Formatdelegates to the inner error'sFormatmethod, andfmt.wrapErrorhas no customFormat, so%+voutput loses the full stack frame rendering thatfundamental.Formatprovides.By embedding
*fundamentaldirectly,wrapErrorpreserves the exactError(),Format(), andStackTrace()behavior thatErrorfhas always returned. The chain stays flat at two levels (wrapError -> cause), andUnwrapreturns the cause directly. The only addition is theUnwrap() errormethod so the wrapped error participates inerrors.Is/errors.As.