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 GitHub
parent 55d62fc1a5
commit 61899398b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,36 +7,27 @@ import (
"google.golang.org/grpc/codes" "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 { func microError(err *errors.Error) codes.Code {
switch err { if err == nil {
case nil:
return codes.OK return codes.OK
} }
switch err.Code { if code, ok := errMapping[err.Code]; ok {
case http.StatusOK: return code
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
} }
return codes.Unknown return codes.Unknown
} }