Skip to content

Commit

Permalink
Removed Error.Is()
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Lindahl committed Apr 9, 2024
1 parent fcc3947 commit 0342a6a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
6 changes: 0 additions & 6 deletions error.go
Expand Up @@ -38,9 +38,3 @@ const (
func (e Error) Error() string {
return string(e)
}

// Is checks if the target error is a UUID Error
func (e Error) Is(target error) bool {
_, ok := target.(*Error)
return ok
}
27 changes: 9 additions & 18 deletions error_test.go
Expand Up @@ -4,42 +4,33 @@ import (
"errors"
"fmt"
"net"
"reflect"
"testing"
)

func TestIsAsError(t *testing.T) {
var e Error
tcs := []struct {
err error
expected string
expectedTarget error
err error
expected string
}{
{
err: fmt.Errorf("%w sample error: %v", ErrInvalidVersion, 123),
expected: "uuid: sample error: 123",
expectedTarget: &e,
err: fmt.Errorf("%w sample error: %v", ErrInvalidVersion, 123),
expected: "uuid: sample error: 123",
},
{
err: fmt.Errorf("%w", ErrInvalidFormat),
expected: "uuid: invalid UUID format",
expectedTarget: ErrInvalidFormat,
err: fmt.Errorf("%w", ErrInvalidFormat),
expected: "uuid: invalid UUID format",
},
{
err: fmt.Errorf("%w %q", ErrIncorrectFormatInString, "test"),
expected: "uuid: incorrect UUID format in string \"test\"",
expectedTarget: ErrIncorrectFormatInString,
err: fmt.Errorf("%w %q", ErrIncorrectFormatInString, "test"),
expected: "uuid: incorrect UUID format in string \"test\"",
},
}
for i, tc := range tcs {
t.Run(fmt.Sprintf("Test case %d", i), func(t *testing.T) {
var e2 Error
if !errors.Is(tc.err, &e2) {
if !errors.As(tc.err, &e2) {
t.Error("expected error to be of a wrapped type of Error")
}
if !errors.Is(tc.err, tc.expectedTarget) {
t.Errorf("expected error to be of type %v, but was %v", reflect.TypeOf(tc.expectedTarget), reflect.TypeOf(tc.err))
}
if tc.err.Error() != tc.expected {
t.Errorf("expected err.Error() to be '%s' but was '%s'", tc.expected, tc.err.Error())
}
Expand Down

0 comments on commit 0342a6a

Please sign in to comment.