Skip to content

Commit

Permalink
Fix Code result for grpc errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Nigmatullin authored and johanbrandhorst committed Apr 7, 2018
1 parent f3652e1 commit 2395146
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func Code(err error) codes.Code {
if err == nil {
return codes.OK
}
if se, ok := err.(interface{ Status() *status.Status }); ok {
return se.Status().Code()
if se, ok := err.(interface{ GRPCStatus() *status.Status }); ok {
return se.GRPCStatus().Code()
}
return codes.Unknown
}
31 changes: 31 additions & 0 deletions status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,37 @@ func TestConvertUnknownError(t *testing.T) {
}
}

func TestCode(t *testing.T) {
tests := []struct {
err error
code codes.Code
}{
{
err: nil,
code: codes.OK,
},
{
err: errors.New("unknown error"),
code: codes.Unknown,
},
{
err: Errorf(codes.Internal, "internal error"),
code: codes.Internal,
},
{
err: Errorf(codes.Unknown, "explicitly unknown error"),
code: codes.Unknown,
},
}

for _, tc := range tests {
code := Code(tc.err)
if code != tc.code {
t.Fatalf("Code(%v) = %v; want %v", tc.err, code, tc.code)
}
}
}

func TestStatus_ErrorDetails(t *testing.T) {
tests := []struct {
code codes.Code
Expand Down

0 comments on commit 2395146

Please sign in to comment.