Skip to content

Commit

Permalink
IsAny multi error support
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitacrit committed May 15, 2024
1 parent c1cc191 commit b0e6c50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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)
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

0 comments on commit b0e6c50

Please sign in to comment.