Skip to content

Commit 720a3ae

Browse files
committed
Walk: add support for go1.20 multi errors (Unwrap() []error)
1 parent 3622ed4 commit 720a3ae

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

errwrap.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type WalkFunc func(error)
2121
// wrapped error in addition to the wrapper itself. Since all the top-level
2222
// functions in errwrap use Walk, this means that all those functions work
2323
// with your custom type.
24+
//
25+
// Note: since Go 1.20 wrappers should instead implement Unwrap() []error
26+
// as documented in package [errors].
2427
type Wrapper interface {
2528
WrappedErrors() []error
2629
}
@@ -150,9 +153,15 @@ func Walk(err error, cb WalkFunc) {
150153
for _, err := range e.WrappedErrors() {
151154
Walk(err, cb)
152155
}
153-
case interface{ Unwrap() error }:
156+
case interface{ Unwrap() error }: // Go 1.13
154157
cb(err)
155158
Walk(e.Unwrap(), cb)
159+
case interface{ Unwrap() []error }: // Go 1.20
160+
cb(err)
161+
162+
for _, err := range e.Unwrap() {
163+
Walk(err, cb)
164+
}
156165
default:
157166
cb(err)
158167
}

0 commit comments

Comments
 (0)