2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2019-06-03 18:44:43 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
)
|
|
|
|
|
2020-08-17 16:10:42 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
func microError(err *errors.Error) codes.Code {
|
2020-08-17 16:10:42 +08:00
|
|
|
if err == nil {
|
2019-06-03 18:44:43 +01:00
|
|
|
return codes.OK
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:10:42 +08:00
|
|
|
if code, ok := errMapping[err.Code]; ok {
|
|
|
|
return code
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
return codes.Unknown
|
|
|
|
}
|