-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptors_test.go
285 lines (248 loc) · 7.37 KB
/
interceptors_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package grpcerrors
import (
"errors"
"reflect"
"testing"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/srvc/fail/v4"
"github.com/srvc/grpc-errors/testing"
)
// Sevice implementations
// ================================================
type emptyService struct {
}
func (s *emptyService) EmptyCall(context.Context, *errorstesting.Empty) (*errorstesting.Empty, error) {
return &errorstesting.Empty{}, nil
}
type errorService struct {
}
func (s *errorService) EmptyCall(context.Context, *errorstesting.Empty) (*errorstesting.Empty, error) {
return nil, errors.New("This error is not wrapped with fail.Error")
}
type failService struct {
}
func (s *failService) EmptyCall(context.Context, *errorstesting.Empty) (*errorstesting.Empty, error) {
return nil, fail.New("This error is wrapped with fail.Error")
}
type ignoredErrorService struct {
}
func (s *ignoredErrorService) EmptyCall(context.Context, *errorstesting.Empty) (*errorstesting.Empty, error) {
return nil, fail.Wrap(errors.New("This error should be ignored"), fail.WithIgnorable())
}
type errorWithStatusService struct {
Code int
}
func (s *errorWithStatusService) EmptyCall(context.Context, *errorstesting.Empty) (*errorstesting.Empty, error) {
return nil, fail.Wrap(errors.New("This error has a status code"), fail.WithCode(s.Code))
}
type errorWithGrpcStatusService struct {
Code codes.Code
}
func (s *errorWithGrpcStatusService) EmptyCall(context.Context, *errorstesting.Empty) (*errorstesting.Empty, error) {
return nil, fail.Wrap(status.Error(s.Code, "This error has a gRPC status code"))
}
// Testings
// ================================================
func Test_UnaryServerInterceptor(t *testing.T) {
cases := []struct {
test string
server errorstesting.TestServiceServer
code codes.Code
errored bool
notWrapped bool
reportable bool
handleNotWrappedError func(err error) error
}{
{
test: "no errors",
server: &emptyService{},
code: codes.OK,
},
{
test: "unwrapped error",
server: &errorService{},
code: codes.Unknown,
errored: true,
notWrapped: true,
reportable: false,
},
{
test: "wrap unwrapped error",
server: &errorService{},
code: codes.Unknown,
errored: true,
notWrapped: true,
reportable: true,
handleNotWrappedError: func(err error) error { return fail.Wrap(err) },
},
{
test: "wrapped error",
server: &failService{},
code: codes.Unknown,
errored: true,
notWrapped: false,
reportable: true,
},
{
test: "ignored error",
server: &ignoredErrorService{},
code: codes.Unknown,
errored: true,
notWrapped: false,
reportable: false,
},
{
test: "error with code that contained CodeMap",
server: &errorWithStatusService{Code: 50},
code: codes.PermissionDenied,
errored: true,
notWrapped: false,
reportable: true,
},
{
test: "error with unknown code",
server: &errorWithStatusService{Code: 51},
code: codes.Unknown,
errored: true,
notWrapped: false,
reportable: true,
},
{
test: "error with code that can handle with CodeMapper",
server: &errorWithStatusService{Code: 52},
code: codes.InvalidArgument,
errored: true,
notWrapped: false,
reportable: true,
},
{
test: "error with gRPC's code",
server: &errorWithGrpcStatusService{Code: codes.AlreadyExists},
code: codes.AlreadyExists,
errored: true,
notWrapped: false,
reportable: true,
},
}
for _, c := range cases {
t.Run(c.test, func(t *testing.T) {
var notWrapped, reportable bool
ctx := errorstesting.CreateTestContext(t)
ctx.Service = c.server
ctx.AddUnaryServerInterceptor(
UnaryServerInterceptor(
WithNotWrappedErrorHandler(func(_ context.Context, err error) error {
notWrapped = true
if c.handleNotWrappedError != nil {
return c.handleNotWrappedError(err)
}
return err
}),
WithReportableErrorHandler(func(_ context.Context, err *fail.Error) error {
reportable = true
return err
}),
WithGrpcStatusUnwrapper(),
WithCodeMap(CodeMap{50: codes.PermissionDenied}),
WithCodeMapper(func(c interface{}) codes.Code {
if c == 52 {
return codes.InvalidArgument
}
return codes.Unknown
}),
),
)
ctx.Setup()
defer ctx.Teardown()
resp, err := ctx.Client.EmptyCall(context.Background(), &errorstesting.Empty{})
if c.errored {
if resp != nil {
t.Error("The request should not return a response")
}
if err == nil {
t.Error("The request should return an error")
}
} else {
if resp == nil {
t.Error("The request should return a response")
}
if err != nil {
t.Error("The request should not return any errors")
}
}
if got, want := notWrapped, c.notWrapped; got != want {
t.Errorf("The returned error is wrapped: got %t, want %t", got, want)
}
if got, want := reportable, c.reportable; got != want {
t.Errorf("The returned error is reportable: got %t, want %t", got, want)
}
if st, ok := status.FromError(err); ok {
if got, want := st.Code(), c.code; got != want {
t.Errorf("The returned error has error code %v, want %v", got, want)
}
} else {
t.Errorf("The returned error does not have error code: %v", err)
}
})
}
}
// Testings for UnaryServerErrorHandler
// ================================================
func Test_UnaryServerInterceptor_WithUnaryServerReportableErrorHandler_WhenAnErrorIsNotAnnotatedWithReport(t *testing.T) {
called := false
req := &errorstesting.Empty{}
ctx := errorstesting.CreateTestContext(t)
ctx.Service = &failService{}
ctx.AddUnaryServerInterceptor(
UnaryServerInterceptor(
WithUnaryServerReportableErrorHandler(func(_ context.Context, gotReq interface{}, info *grpc.UnaryServerInfo, err *fail.Error) error {
called = true
return err
}),
),
)
ctx.Setup()
defer ctx.Teardown()
resp, err := ctx.Client.EmptyCall(context.Background(), req)
if resp != nil {
t.Error("The request should not return any responses")
}
if err == nil {
t.Error("The request should return an error")
}
if !called {
t.Error("Report error handler should not be called")
}
}
func Test_UnaryServerInterceptor_WithUnaryServerReportableErrorHandler_WhenAnErrorIsAnnotatedWithIgnored(t *testing.T) {
called := false
req := &errorstesting.Empty{}
ctx := errorstesting.CreateTestContext(t)
ctx.Service = &ignoredErrorService{}
ctx.AddUnaryServerInterceptor(
UnaryServerInterceptor(
WithUnaryServerReportableErrorHandler(func(_ context.Context, gotReq interface{}, info *grpc.UnaryServerInfo, err *fail.Error) error {
called = true
if got, want := gotReq, req; !reflect.DeepEqual(got, want) {
t.Errorf("Received request is %v, want %v", got, want)
}
return err
}),
),
)
ctx.Setup()
defer ctx.Teardown()
resp, err := ctx.Client.EmptyCall(context.Background(), req)
if resp != nil {
t.Error("The request should not return any responses")
}
if err == nil {
t.Error("The request should return an error")
}
if called {
t.Error("Report error handler should be called")
}
}