Skip to content
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

IsAny() multi error support #140

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions markers/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ func IsAny(err error, references ...error) bool {
return true
}
}

// Recursively try multi-error causes, if applicable.
for _, me := range errbase.UnwrapMulti(c) {
if IsAny(me, references...) {
return true
}
}
}

// Try harder with marks.
Expand Down
12 changes: 12 additions & 0 deletions markers/markers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ func TestIsAny(t *testing.T) {
err2 := errors.New("world")
err3 := pkgErr.Wrap(err1, "world")
err4 := pkgErr.Wrap(err2, "universe")
err5 := errors.Join(err1, errors.New("gopher"))
err6 := errors.Join(errors.New("gopher"), err2)
err7 := errors.Join(err1, err2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case that wraps at the top level? That will ensure that the implementation is necessary within the UnwrapOnce loop instead of outside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

err8 := pkgErr.Wrap(err7, "gopher")
var nilErr error

tt.Check(markers.IsAny(err1, err1))
Expand All @@ -371,6 +375,14 @@ func TestIsAny(t *testing.T) {
tt.Check(markers.IsAny(err3, err2, nilErr, err1))
tt.Check(markers.IsAny(nilErr, err2, nilErr, err1))
tt.Check(!markers.IsAny(nilErr, err2, err1))
tt.Check(markers.IsAny(err5, err1))
tt.Check(markers.IsAny(err6, err2))
tt.Check(markers.IsAny(err7, err1))
tt.Check(markers.IsAny(err7, err2))
tt.Check(markers.IsAny(err7, err1, err2))
tt.Check(markers.IsAny(err8, err1))
tt.Check(markers.IsAny(err8, err2))
tt.Check(markers.IsAny(err8, err1, err2))
}

// This test demonstrates that two errors that are structurally
Expand Down
Loading