feat: convert grpc errors to http status codes (#1997)

* feat: convert grpc errors to http status codes

* chore: circuit break include unimplemented grpc error

* chore: add reference link in comments
This commit is contained in:
Kevin Wan
2022-06-11 23:07:26 +08:00
committed by GitHub
parent db9a1f3e27
commit ed1c937998
6 changed files with 197 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
package errcode
import (
"net/http"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// CodeFromGrpcError converts the gRPC error to an HTTP status code.
// See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
func CodeFromGrpcError(err error) int {
code := status.Code(err)
switch code {
case codes.OK:
return http.StatusOK
case codes.InvalidArgument, codes.FailedPrecondition, codes.OutOfRange:
return http.StatusBadRequest
case codes.Unauthenticated:
return http.StatusUnauthorized
case codes.PermissionDenied:
return http.StatusForbidden
case codes.NotFound:
return http.StatusNotFound
case codes.Canceled:
return http.StatusRequestTimeout
case codes.AlreadyExists, codes.Aborted:
return http.StatusConflict
case codes.ResourceExhausted:
return http.StatusTooManyRequests
case codes.Internal, codes.DataLoss, codes.Unknown:
return http.StatusInternalServerError
case codes.Unimplemented:
return http.StatusNotImplemented
case codes.Unavailable:
return http.StatusServiceUnavailable
case codes.DeadlineExceeded:
return http.StatusGatewayTimeout
}
return http.StatusInternalServerError
}
// IsGrpcError checks if the error is a gRPC error.
func IsGrpcError(err error) bool {
if err == nil {
return false
}
_, ok := err.(interface {
GRPCStatus() *status.Status
})
return ok
}

View File

@@ -0,0 +1,123 @@
package errcode
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestCodeFromGrpcError(t *testing.T) {
tests := []struct {
name string
code codes.Code
want int
}{
{
name: "OK",
code: codes.OK,
want: http.StatusOK,
},
{
name: "Invalid argument",
code: codes.InvalidArgument,
want: http.StatusBadRequest,
},
{
name: "Failed precondition",
code: codes.FailedPrecondition,
want: http.StatusBadRequest,
},
{
name: "Out of range",
code: codes.OutOfRange,
want: http.StatusBadRequest,
},
{
name: "Unauthorized",
code: codes.Unauthenticated,
want: http.StatusUnauthorized,
},
{
name: "Permission denied",
code: codes.PermissionDenied,
want: http.StatusForbidden,
},
{
name: "Not found",
code: codes.NotFound,
want: http.StatusNotFound,
},
{
name: "Canceled",
code: codes.Canceled,
want: http.StatusRequestTimeout,
},
{
name: "Already exists",
code: codes.AlreadyExists,
want: http.StatusConflict,
},
{
name: "Aborted",
code: codes.Aborted,
want: http.StatusConflict,
},
{
name: "Resource exhausted",
code: codes.ResourceExhausted,
want: http.StatusTooManyRequests,
},
{
name: "Internal",
code: codes.Internal,
want: http.StatusInternalServerError,
},
{
name: "Data loss",
code: codes.DataLoss,
want: http.StatusInternalServerError,
},
{
name: "Unknown",
code: codes.Unknown,
want: http.StatusInternalServerError,
},
{
name: "Unimplemented",
code: codes.Unimplemented,
want: http.StatusNotImplemented,
},
{
name: "Unavailable",
code: codes.Unavailable,
want: http.StatusServiceUnavailable,
},
{
name: "Deadline exceeded",
code: codes.DeadlineExceeded,
want: http.StatusGatewayTimeout,
},
{
name: "Beyond defined error",
code: codes.Code(^uint32(0)),
want: http.StatusInternalServerError,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.want, CodeFromGrpcError(status.Error(test.code, "foo")))
})
}
}
func TestIsGrpcError(t *testing.T) {
assert.True(t, IsGrpcError(status.Error(codes.Unknown, "foo")))
assert.False(t, IsGrpcError(errors.New("foo")))
assert.False(t, IsGrpcError(nil))
}