diff options
| author | Damien Neil <dneil@google.com> | 2022-09-22 10:43:26 -0700 |
|---|---|---|
| committer | Damien Neil <dneil@google.com> | 2022-09-29 18:40:40 +0000 |
| commit | 4a0a2b33dfa3c99250efa222439f2c27d6780e4a (patch) | |
| tree | 25b1d2541b1b1244fe86d3b7ab690bff475e7ac7 /src/fmt/errors.go | |
| parent | 36f046d934c66fb6eb47d568e04665708c096ad7 (diff) | |
| download | go-4a0a2b33dfa3c99250efa222439f2c27d6780e4a.tar.xz | |
errors, fmt: add support for wrapping multiple errors
An error which implements an "Unwrap() []error" method wraps all the
non-nil errors in the returned []error.
We replace the concept of the "error chain" inspected by errors.Is
and errors.As with the "error tree". Is and As perform a pre-order,
depth-first traversal of an error's tree. As returns the first
matching result, if any.
The new errors.Join function returns an error wrapping a list of errors.
The fmt.Errorf function now supports multiple instances of the %w verb.
For #53435.
Change-Id: Ib7402e70b68e28af8f201d2b66bd8e87ccfb5283
Reviewed-on: https://go-review.googlesource.com/c/go/+/432898
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Diffstat (limited to 'src/fmt/errors.go')
| -rw-r--r-- | src/fmt/errors.go | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/src/fmt/errors.go b/src/fmt/errors.go index 4f4daf19e1..1fbd39f8f1 100644 --- a/src/fmt/errors.go +++ b/src/fmt/errors.go @@ -4,26 +4,48 @@ package fmt -import "errors" +import ( + "errors" + "sort" +) // Errorf formats according to a format specifier and returns the string as a // value that satisfies error. // // If the format specifier includes a %w verb with an error operand, -// the returned error will implement an Unwrap method returning the operand. It is -// invalid to include more than one %w verb or to supply it with an operand -// that does not implement the error interface. The %w verb is otherwise -// a synonym for %v. +// the returned error will implement an Unwrap method returning the operand. +// If there is more than one %w verb, the returned error will implement an +// Unwrap method returning a []error containing all the %w operands in the +// order they appear in the arguments. +// It is invalid to supply the %w verb with an operand that does not implement +// the error interface. The %w verb is otherwise a synonym for %v. func Errorf(format string, a ...any) error { p := newPrinter() p.wrapErrs = true p.doPrintf(format, a) s := string(p.buf) var err error - if p.wrappedErr == nil { + switch len(p.wrappedErrs) { + case 0: err = errors.New(s) - } else { - err = &wrapError{s, p.wrappedErr} + case 1: + w := &wrapError{msg: s} + w.err, _ = a[p.wrappedErrs[0]].(error) + err = w + default: + if p.reordered { + sort.Ints(p.wrappedErrs) + } + var errs []error + for i, argNum := range p.wrappedErrs { + if i > 0 && p.wrappedErrs[i-1] == argNum { + continue + } + if e, ok := a[argNum].(error); ok { + errs = append(errs, e) + } + } + err = &wrapErrors{s, errs} } p.free() return err @@ -41,3 +63,16 @@ func (e *wrapError) Error() string { func (e *wrapError) Unwrap() error { return e.err } + +type wrapErrors struct { + msg string + errs []error +} + +func (e *wrapErrors) Error() string { + return e.msg +} + +func (e *wrapErrors) Unwrap() []error { + return e.errs +} |
