@@ -52,6 +52,14 @@ func (s *errorWithStatusService) EmptyCall(context.Context, *errorstesting.Empty
52
52
return nil , apperrors .WithStatusCode (errors .New ("This error has a status code" ), s .Code )
53
53
}
54
54
55
+ type errorWithGrpcStatusService struct {
56
+ Code codes.Code
57
+ }
58
+
59
+ func (s * errorWithGrpcStatusService ) EmptyCall (context.Context , * errorstesting.Empty ) (* errorstesting.Empty , error ) {
60
+ return nil , apperrors .Wrap (status .Error (s .Code , "This error has a gRPC status code" ))
61
+ }
62
+
55
63
// Testings
56
64
// ================================================
57
65
func Test_UnaryServerInterceptor_WhenDoesNotRespondErrors (t * testing.T ) {
@@ -279,6 +287,38 @@ func Test_UnaryServerInterceptor_WithStatusCodeMap_WhenUnknownCode(t *testing.T)
279
287
}
280
288
}
281
289
290
+ func Test_UnaryServerInterceptor_WithStatusCodeMap_WhenAlreadySet (t * testing.T ) {
291
+ code := codes .Unauthenticated
292
+
293
+ ctx := errorstesting .CreateTestContext (t )
294
+ ctx .Service = & errorWithGrpcStatusService {Code : code }
295
+ ctx .AddUnaryServerInterceptor (
296
+ UnaryServerInterceptor (
297
+ WithStatusCodeMap (map [int ]codes.Code {
298
+ int (code ): codes .Aborted ,
299
+ }),
300
+ ),
301
+ )
302
+ ctx .Setup ()
303
+ defer ctx .Teardown ()
304
+
305
+ resp , err := ctx .Client .EmptyCall (context .Background (), & errorstesting.Empty {})
306
+
307
+ if resp != nil {
308
+ t .Error ("The request should not return any responses" )
309
+ }
310
+
311
+ if err == nil {
312
+ t .Error ("The request should return an error" )
313
+ }
314
+
315
+ if st , ok := status .FromError (err ); ! ok {
316
+ t .Error ("Returned error should has status code" )
317
+ } else if got , want := st .Code (), code ; got != want {
318
+ t .Errorf ("Returned error had status code %v, want %v" , got , want )
319
+ }
320
+ }
321
+
282
322
func Test_UnaryServerInterceptor_WithStatusCodeMapper (t * testing.T ) {
283
323
code := 50
284
324
mappedCode := codes .Unavailable
0 commit comments