Skip to content

Walk: add support for go1.20 multi errors (Unwrap() []error) #18

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion errwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type WalkFunc func(error)
// wrapped error in addition to the wrapper itself. Since all the top-level
// functions in errwrap use Walk, this means that all those functions work
// with your custom type.
//
// Note: since Go 1.20 wrappers should instead implement Unwrap() []error
// as documented in package [errors] and [errors.Join].
type Wrapper interface {
WrappedErrors() []error
}
Expand Down Expand Up @@ -150,9 +153,15 @@ func Walk(err error, cb WalkFunc) {
for _, err := range e.WrappedErrors() {
Walk(err, cb)
}
case interface{ Unwrap() error }:
case interface{ Unwrap() error }: // Go 1.13
cb(err)
Walk(e.Unwrap(), cb)
case interface{ Unwrap() []error }: // Go 1.20
cb(err)

for _, err := range e.Unwrap() {
Walk(err, cb)
}
default:
cb(err)
}
Expand Down