-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
130 lines (97 loc) · 3.43 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package grpc_server
import (
"context"
"fmt"
"testing"
"github.com/moveaxlab/go-grpc-server/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type testApplicationError struct {
message string
grpcCode codes.Code
code string
}
type GRPCStatus interface {
GRPCStatus() *status.Status
}
func (e *testApplicationError) Error() string {
return e.message
}
func (e *testApplicationError) GRPCStatus() *status.Status {
return status.New(e.grpcCode, e.message)
}
func (e *testApplicationError) Trailer() metadata.MD {
return metadata.New(map[string]string{
"code": e.code,
})
}
func TestErrorInterceptor(t *testing.T) {
t.Run("works when there is no error", func(t *testing.T) {
client, mockServer, cleanup := setupTestServer(t, NewErrorInterceptor())
defer cleanup()
ctx := context.Background()
mockServer.On("Endpoint", mock.Anything, mock.Anything).Return(&internal.Output{Value: "World"}, nil)
res, err := client.Endpoint(ctx, &internal.Input{Value: "Hello"})
assert.Nil(t, err)
assert.Equal(t, "World", res.Value)
})
t.Run("handles application errors", func(t *testing.T) {
client, mockServer, cleanup := setupTestServer(t, NewErrorInterceptor())
defer cleanup()
ctx := context.Background()
applicationError := &testApplicationError{
message: "Failed",
grpcCode: codes.InvalidArgument,
code: "APPLICATION_ERROR",
}
mockServer.On("Endpoint", mock.Anything, mock.Anything).Return(nil, applicationError)
var md metadata.MD
_, err := client.Endpoint(ctx, &internal.Input{Value: "Hello"}, grpc.Trailer(&md))
assert.NotNil(t, err)
grpcErr, ok := err.(GRPCStatus)
assert.True(t, ok)
assert.Equal(t, "Failed", grpcErr.GRPCStatus().Message())
assert.Equal(t, codes.InvalidArgument, grpcErr.GRPCStatus().Code())
assert.Equal(t, []string{"APPLICATION_ERROR"}, md["code"])
})
t.Run("handles application panics", func(t *testing.T) {
client, mockServer, cleanup := setupTestServer(t, NewErrorInterceptor(), RecoverInterceptor)
defer cleanup()
ctx := context.Background()
applicationError := &testApplicationError{
message: "Failed",
grpcCode: codes.InvalidArgument,
code: "APPLICATION_ERROR",
}
mockServer.On("Endpoint", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
panic(applicationError)
})
var md metadata.MD
_, err := client.Endpoint(ctx, &internal.Input{Value: "Hello"}, grpc.Trailer(&md))
assert.NotNil(t, err)
grpcErr, ok := err.(GRPCStatus)
assert.True(t, ok)
assert.Equal(t, "Failed", grpcErr.GRPCStatus().Message())
assert.Equal(t, codes.InvalidArgument, grpcErr.GRPCStatus().Code())
assert.Equal(t, []string{"APPLICATION_ERROR"}, md["code"])
})
t.Run("does nothing on other errors", func(t *testing.T) {
client, mockServer, cleanup := setupTestServer(t, NewErrorInterceptor())
defer cleanup()
ctx := context.Background()
mockServer.On("Endpoint", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("random error"))
var md metadata.MD
_, err := client.Endpoint(ctx, &internal.Input{Value: "Hello"}, grpc.Trailer(&md))
assert.NotNil(t, err)
grpcErr, ok := err.(GRPCStatus)
assert.True(t, ok)
assert.Equal(t, "random error", grpcErr.GRPCStatus().Message())
assert.Equal(t, codes.Unknown, grpcErr.GRPCStatus().Code())
assert.Nil(t, md["code"])
})
}