simplifies code (#1934)

Co-authored-by: 刘海洋 <haiyang@snqu.com>
This commit is contained in:
zuoan 2020-08-17 16:10:42 +08:00 committed by Vasiliy Tolstov
parent ed139b1fdd
commit 03af0bddc6

View File

@ -7,36 +7,27 @@ import (
"google.golang.org/grpc/codes"
)
var errMapping = map[int32]codes.Code{
http.StatusOK: codes.OK,
http.StatusBadRequest: codes.InvalidArgument,
http.StatusRequestTimeout: codes.DeadlineExceeded,
http.StatusNotFound: codes.NotFound,
http.StatusConflict: codes.AlreadyExists,
http.StatusForbidden: codes.PermissionDenied,
http.StatusUnauthorized: codes.Unauthenticated,
http.StatusPreconditionFailed: codes.FailedPrecondition,
http.StatusNotImplemented: codes.Unimplemented,
http.StatusInternalServerError: codes.Internal,
http.StatusServiceUnavailable: codes.Unavailable,
}
func microError(err *errors.Error) codes.Code {
switch err {
case nil:
if err == nil {
return codes.OK
}
switch err.Code {
case http.StatusOK:
return codes.OK
case http.StatusBadRequest:
return codes.InvalidArgument
case http.StatusRequestTimeout:
return codes.DeadlineExceeded
case http.StatusNotFound:
return codes.NotFound
case http.StatusConflict:
return codes.AlreadyExists
case http.StatusForbidden:
return codes.PermissionDenied
case http.StatusUnauthorized:
return codes.Unauthenticated
case http.StatusPreconditionFailed:
return codes.FailedPrecondition
case http.StatusNotImplemented:
return codes.Unimplemented
case http.StatusInternalServerError:
return codes.Internal
case http.StatusServiceUnavailable:
return codes.Unavailable
if code, ok := errMapping[err.Code]; ok {
return code
}
return codes.Unknown
}