Skip to content

Commit

Permalink
Add the ability to check if an error stack contains an error value.
Browse files Browse the repository at this point in the history
  • Loading branch information
deitrix committed Dec 12, 2019
1 parent cae37a5 commit 29217c2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
28 changes: 20 additions & 8 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,34 @@ func FieldsSlice(err error) []interface{} {
return fields
}

// Is reports whether err is an *Error of the given Kind(s). If the given error is an *Error, but
// it has an empty kind, it will check the cause of that error recursively. If error is not an
// *Error, or is nil, Is will return false.
func Is(err error, kind ...Kind) bool {
// Is reports whether the err is an *Error of the given kind/value. If the given kind is of type Kind/string, it will be
// checked against the error's Kind. If the given kind is of any other type, it will be checked against the error's
// cause. This is done recursively until a matching error is found. Calling Is with multiple kinds reports whether the
// error is one of the given kind/values, not all of.
func Is(err error, kind ...interface{}) bool {
if err == nil {
return false
}

e, ok := err.(*Error)
if ok && e.Kind != "" {
for _, k := range kind {
if e.Kind == k {
if !ok {
return false
}

for _, k := range kind {
switch val := k.(type) {
case Kind, string:
if e.Kind == val {
return true
}
default:
if e.Cause == val {
return true
}
}
} else if ok && e.Cause != nil {
}

if e.Cause != nil {
return Is(e.Cause, kind...)
}

Expand Down
17 changes: 17 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errors

import (
"context"
"errors"
"strings"
"testing"
Expand Down Expand Up @@ -243,9 +244,17 @@ func TestFieldsSlice(t *testing.T) {
})
}

type errorType struct {}

func (t errorType) Error() string {
return "error!"
}


func TestIs(t *testing.T) {
kind1 := Kind("testing 1")
kind2 := Kind("testing 2")
kind3 := errorType{}

t.Run("should return false on nil error", func(t *testing.T) {
assert.False(t, Is(nil))
Expand Down Expand Up @@ -280,6 +289,14 @@ func TestIs(t *testing.T) {
err := Wrap(New(kind1))
assert.True(t, Is(err, kind2, kind1))
})

t.Run("should also be able to check value types", func(t *testing.T) {
err := Wrap(kind3)
assert.True(t, Is(err, kind3))

err = Wrap(context.Canceled)
assert.True(t, Is(err, context.Canceled))
})
}

func TestMessage(t *testing.T) {
Expand Down

0 comments on commit 29217c2

Please sign in to comment.