diff --git a/errors.go b/errors.go index 1231f36..fe6f802 100644 --- a/errors.go +++ b/errors.go @@ -437,6 +437,9 @@ func Propagate(err error) error { // signature requires an error to test against, and checking against terrors would // requite creating a new terror with the specific code. func Is(err error, code ...string) bool { + if len(code) == 0 { + return false + } switch err := err.(type) { case *Error: if err.PrefixMatches(code...) { diff --git a/errors_test.go b/errors_test.go index 5d2c230..c7ba4e2 100644 --- a/errors_test.go +++ b/errors_test.go @@ -48,9 +48,9 @@ func TestErrorConstructors(t *testing.T) { }, { Unauthorized, "service.foo", "test params", map[string]string{ - "some key": "some value", - "another key": "another value", - }, ErrUnauthorized, + "some key": "some value", + "another key": "another value", + }, ErrUnauthorized, }, { PreconditionFailed, "service.foo", "precondition_failed.service.foo", nil, ErrPreconditionFailed, @@ -624,3 +624,14 @@ func TestCircularErrorProducesFiniteOutputWithoutStackFrames(t *testing.T) { // There's no actual stack in the causal cycle, so we don't render anything here. assert.Empty(t, ss) } +func TestIsEmptyCodeReturnsFalse(t *testing.T) { + // For any terror error, calling Is(err) with no codes should be false + err := InternalService("x", "msg", nil) + if Is(err) { + t.Errorf("expected Is(err) to be false when no codes provided") + } + err2 := NotFound("y", "msg", nil) + if Is(err2) { + t.Errorf("expected Is(err2) to be false when no codes provided") + } +}