Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented support for checkable errors #131

Merged
merged 9 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 0 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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) {
PatrLind marked this conversation as resolved.
Show resolved Hide resolved
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
Loading