Skip to content

Commit e868cc4

Browse files
authored
Merge pull request #16 from izumin5210/izumin5210/status-unwrapper
Impl WithGrpcStatusUnwrapper
2 parents eeaa080 + b02005a commit e868cc4

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

handlers.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,10 @@ func WithStatusCodeMap(m map[int]codes.Code) interface {
9999
StreamServerErrorHandler
100100
} {
101101
return WithAppErrorHandler(func(c context.Context, err *apperrors.Error) error {
102-
newCode := codes.Unknown
103-
if s, ok := status.FromError(err.Err); ok {
104-
newCode = s.Code()
105-
} else if c, ok := m[err.StatusCode]; ok {
106-
newCode = c
102+
if c, ok := m[err.StatusCode]; ok {
103+
return status.Error(c, err.Error())
107104
}
108-
return status.Error(newCode, err.Error())
105+
return err
109106
})
110107
}
111108

@@ -118,3 +115,16 @@ func WithStatusCodeMapper(mapFn func(code int) codes.Code) interface {
118115
return status.Error(mapFn(err.StatusCode), err.Error())
119116
})
120117
}
118+
119+
// WithGrpcStatusUnwrapper returns unwrapped error if this has a gRPC status.
120+
func WithGrpcStatusUnwrapper() interface {
121+
UnaryServerErrorHandler
122+
StreamServerErrorHandler
123+
} {
124+
return WithAppErrorHandler(func(c context.Context, err *apperrors.Error) error {
125+
if _, ok := status.FromError(err.Err); ok {
126+
return err.Err
127+
}
128+
return err
129+
})
130+
}

interceptors_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,16 @@ func Test_UnaryServerInterceptor_WithStatusCodeMap_WhenUnknownCode(t *testing.T)
287287
}
288288
}
289289

290-
func Test_UnaryServerInterceptor_WithStatusCodeMap_WhenAlreadySet(t *testing.T) {
290+
func Test_UnaryServerInterceptor_WithGrpcStatusUnwrapper(t *testing.T) {
291291
code := codes.Unauthenticated
292292

293293
ctx := errorstesting.CreateTestContext(t)
294294
ctx.Service = &errorWithGrpcStatusService{Code: code}
295295
ctx.AddUnaryServerInterceptor(
296296
UnaryServerInterceptor(
297+
WithGrpcStatusUnwrapper(),
297298
WithStatusCodeMap(map[int]codes.Code{
298-
int(code): codes.Aborted,
299+
50: codes.Unavailable,
299300
}),
300301
),
301302
)
@@ -319,6 +320,38 @@ func Test_UnaryServerInterceptor_WithStatusCodeMap_WhenAlreadySet(t *testing.T)
319320
}
320321
}
321322

323+
func Test_UnaryServerInterceptor_WithGrpcStatusUnwrapper_WithoutGrpcStatus(t *testing.T) {
324+
code := 50
325+
ctx := errorstesting.CreateTestContext(t)
326+
ctx.Service = &errorWithStatusService{Code: code}
327+
ctx.AddUnaryServerInterceptor(
328+
UnaryServerInterceptor(
329+
WithGrpcStatusUnwrapper(),
330+
WithStatusCodeMap(map[int]codes.Code{
331+
code: codes.Unauthenticated,
332+
}),
333+
),
334+
)
335+
ctx.Setup()
336+
defer ctx.Teardown()
337+
338+
resp, err := ctx.Client.EmptyCall(context.Background(), &errorstesting.Empty{})
339+
340+
if resp != nil {
341+
t.Error("The request should not return any responses")
342+
}
343+
344+
if err == nil {
345+
t.Error("The request should return an error")
346+
}
347+
348+
if st, ok := status.FromError(err); !ok {
349+
t.Error("Returned error should has status code")
350+
} else if got, want := st.Code(), codes.Unauthenticated; got != want {
351+
t.Errorf("Returned error had status code %v, want %v", got, want)
352+
}
353+
}
354+
322355
func Test_UnaryServerInterceptor_WithStatusCodeMapper(t *testing.T) {
323356
code := 50
324357
mappedCode := codes.Unavailable

0 commit comments

Comments
 (0)