diff --git a/error.go b/error.go index 83046a5..22a42b8 100644 --- a/error.go +++ b/error.go @@ -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 }