Skip to content
Open
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
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...) {
Expand Down
17 changes: 14 additions & 3 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
}