Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Add errio.Equals function to compare the namespace and code of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBarendse committed Sep 4, 2019
1 parent eaf733c commit 858988f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
14 changes: 2 additions & 12 deletions internals/api/uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (

// Errors
var (
errInvalidUUID = errio.Namespace("uuid").Code("invalid")
ErrInvalidUUIDErr = errInvalidUUID.ErrorPref("invalid uuid: %s")
ErrInvalidUUID = errio.Namespace("uuid").Code("invalid").ErrorPref("invalid uuid: %s")
)

// UUID is a wrapper around go.uuid.UUID
Expand All @@ -28,7 +27,7 @@ func New() *UUID {
func FromString(str string) (*UUID, error) {
id, err := gid.FromString(str)
if err != nil {
return nil, ErrInvalidUUIDErr(err)
return nil, ErrInvalidUUID(err)
}
return &UUID{id}, nil
}
Expand All @@ -53,12 +52,3 @@ func Validate(str string) error {
_, err := FromString(str)
return err
}

// IsErrInvalidUUID returns whether the given error is returned for an invalid uuid.
func IsErrInvalidUUID(err error) bool {
publicErr, ok := err.(errio.PublicError)
if !ok {
return false
}
return publicErr.Namespace == errInvalidUUID.Namespace && publicErr.Code == errInvalidUUID.Code
}
9 changes: 9 additions & 0 deletions internals/errio/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,12 @@ func Wrap(base PublicStatusError, errs ...error) PublicStatusError {

return base
}

// Equals returns whether the errors have matching namespace and code.
func Equals(a PublicError, b error) bool {
publicError, ok := b.(PublicError)
if !ok {
return false
}
return a.Namespace == publicError.Namespace && a.Code == publicError.Code
}
39 changes: 39 additions & 0 deletions internals/errio/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"reflect"
"testing"

"github.com/secrethub/secrethub-go/internals/assert"
)

var (
Expand Down Expand Up @@ -112,3 +114,40 @@ func TestUnexpectedStatusError(t *testing.T) {
t.Error("returned error does not contain a message")
}
}

func TestEquals(t *testing.T) {
cases := map[string]struct {
a PublicError
b error
expected bool
}{
"completely equal errors": {
a: Namespace("app").Code("invalid foo").Error("invalid value for foo: value should be a string"),
b: Namespace("app").Code("invalid foo").Error("invalid value for foo: value should be a string"),
expected: true,
},
"different error message": {
a: Namespace("app").Code("invalid value").Error("invalid value for foo: value should be a string"),
b: Namespace("app").Code("invalid value").Error("invalid value for foo: value cannot contain special characters"),
expected: true,
},
"different namespace": {
a: Namespace("app").Code("invalid foo").Error("invalid value for foo: value should be a string"),
b: Namespace("foo").Code("invalid foo").Error("invalid value for foo: value should be a string"),
expected: false,
},
"different code": {
a: Namespace("app").Code("invalid foo").Error("invalid value for foo: value should be a string"),
b: Namespace("app").Code("unauthenticated").Error("request is not authenticated"),
expected: false,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
actual := Equals(tc.a, tc.b)

assert.Equal(t, actual, tc.expected)
})
}
}

0 comments on commit 858988f

Please sign in to comment.