diff --git a/errors/errors_test.go b/errors/errors_test.go index 1408ec67d..32f6f2d26 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -6,6 +6,7 @@ import ( "net/http" "testing" + "github.com/stretchr/testify/assert" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/grpc/test/grpc_testing" @@ -57,4 +58,45 @@ func TestError(t *testing.T) { if se2.Code != http.StatusBadRequest { t.Errorf("convert code err, got %d want %d", UnknownCode, http.StatusBadRequest) } + assert.Nil(t, FromError(nil)) + e := FromError(errors.New("test")) + assert.Equal(t, e.Code, int32(UnknownCode)) +} + +func TestIs(t *testing.T) { + tests := []struct { + name string + e *Error + err error + want bool + }{ + { + name: "true", + e: &Error{Reason: "test"}, + err: New(http.StatusNotFound, "test", ""), + want: true, + }, + { + name: "false", + e: &Error{Reason: "test"}, + err: errors.New("test"), + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if ok := tt.e.Is(tt.err); ok != tt.want { + t.Errorf("Error.Error() = %v, want %v", ok, tt.want) + } + }) + } +} + +func TestOther(t *testing.T) { + assert.Equal(t, Code(nil), 200) + assert.Equal(t, Code(errors.New("test")), UnknownCode) + assert.Equal(t, Reason(errors.New("test")), UnknownReason) + err := Errorf(10001, "test code 10001", "message") + assert.Equal(t, Code(err), 10001) + assert.Equal(t, Reason(err), "test code 10001") } diff --git a/errors/types_test.go b/errors/types_test.go index 94841f031..e45c52aae 100644 --- a/errors/types_test.go +++ b/errors/types_test.go @@ -1,6 +1,8 @@ package errors -import "testing" +import ( + "testing" +) func TestTypes(t *testing.T) { var ( @@ -12,6 +14,8 @@ func TestTypes(t *testing.T) { Conflict("reason_409", "message_409"), InternalServer("reason_500", "message_500"), ServiceUnavailable("reason_503", "message_503"), + GatewayTimeout("reason_504", "message_504"), + ClientClosed("reason_499", "message_499"), } output = []func(error) bool{ IsBadRequest, @@ -21,6 +25,8 @@ func TestTypes(t *testing.T) { IsConflict, IsInternalServer, IsServiceUnavailable, + IsGatewayTimeout, + IsClientClosed, } ) diff --git a/errors/wrap_test.go b/errors/wrap_test.go new file mode 100644 index 000000000..8be90250f --- /dev/null +++ b/errors/wrap_test.go @@ -0,0 +1,23 @@ +package errors + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +type mockErr struct{} + +func (*mockErr) Error() string { + return "mock error" +} + +func TestWarp(t *testing.T) { + var err error = &mockErr{} + err2 := fmt.Errorf("wrap %w", err) + assert.Equal(t, err, Unwrap(err2)) + assert.True(t, Is(err2, err)) + err3 := &mockErr{} + assert.True(t, As(err2, &err3)) +}