Skip to content

Commit

Permalink
feat: allow to get all tags in wrapped errors
Browse files Browse the repository at this point in the history
  • Loading branch information
matdurand authored and Southclaws committed Sep 15, 2023
1 parent f02308e commit 568e1b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ftag/ftag.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ func Get(err error) Kind {
return Internal
}

func GetAll(err error) []Kind {
if err == nil {
return nil
}

var ks []Kind
for err != nil {
if f, ok := err.(*withKind); ok {
ks = append(ks, f.tag)
}

err = errors.Unwrap(err)
}

return ks
}

// Common kinds of error:

const (
Expand Down
9 changes: 9 additions & 0 deletions tests/ftag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ func TestWrapWithKindChanging(t *testing.T) {

assert.Equal(t, ftag.NotFound, out, "Should always pick the most recent kind from an error chain.")
}

func TestMultipleWrappedKind(t *testing.T) {
err := ftag.Wrap(errors.New("a problem"), ftag.Internal)
err = ftag.Wrap(err, ftag.InvalidArgument)
err = ftag.Wrap(err, ftag.NotFound)
out := ftag.GetAll(err)

assert.Equal(t, []ftag.Kind{ftag.NotFound, ftag.InvalidArgument, ftag.Internal}, out)
}

0 comments on commit 568e1b7

Please sign in to comment.