You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.2 KiB
94 lines
2.2 KiB
1 year ago
|
package errcode
|
||
2 years ago
|
|
||
|
import (
|
||
|
"google.golang.org/grpc/codes"
|
||
1 year ago
|
"google.golang.org/grpc/status"
|
||
2 years ago
|
"net/http"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
ClientClosed = 499
|
||
|
)
|
||
|
|
||
1 year ago
|
func ToGRPCCode(code int) codes.Code {
|
||
2 years ago
|
switch code {
|
||
|
case http.StatusOK:
|
||
|
return codes.OK
|
||
|
case http.StatusBadRequest:
|
||
|
return codes.InvalidArgument
|
||
|
case http.StatusUnauthorized:
|
||
|
return codes.Unauthenticated
|
||
|
case http.StatusForbidden:
|
||
|
return codes.PermissionDenied
|
||
|
case http.StatusNotFound:
|
||
|
return codes.NotFound
|
||
|
case http.StatusConflict:
|
||
|
return codes.Aborted
|
||
|
case http.StatusTooManyRequests:
|
||
|
return codes.ResourceExhausted
|
||
|
case http.StatusInternalServerError:
|
||
|
return codes.Internal
|
||
|
case http.StatusNotImplemented:
|
||
|
return codes.Unimplemented
|
||
|
case http.StatusServiceUnavailable:
|
||
|
return codes.Unavailable
|
||
|
case http.StatusGatewayTimeout:
|
||
|
return codes.DeadlineExceeded
|
||
|
case ClientClosed:
|
||
|
return codes.Canceled
|
||
|
}
|
||
|
return codes.Unknown
|
||
|
}
|
||
1 year ago
|
func FromGRPCCode(code codes.Code) int {
|
||
2 years ago
|
switch code {
|
||
|
case codes.OK:
|
||
|
return http.StatusOK
|
||
|
case codes.Canceled:
|
||
|
return ClientClosed
|
||
|
case codes.Unknown:
|
||
|
return http.StatusInternalServerError
|
||
|
case codes.InvalidArgument:
|
||
|
return http.StatusBadRequest
|
||
|
case codes.DeadlineExceeded:
|
||
|
return http.StatusGatewayTimeout
|
||
|
case codes.NotFound:
|
||
|
return http.StatusNotFound
|
||
|
case codes.AlreadyExists:
|
||
|
return http.StatusConflict
|
||
|
case codes.PermissionDenied:
|
||
|
return http.StatusForbidden
|
||
|
case codes.Unauthenticated:
|
||
|
return http.StatusUnauthorized
|
||
|
case codes.ResourceExhausted:
|
||
|
return http.StatusTooManyRequests
|
||
|
case codes.FailedPrecondition:
|
||
|
return http.StatusBadRequest
|
||
|
case codes.Aborted:
|
||
|
return http.StatusConflict
|
||
|
case codes.OutOfRange:
|
||
|
return http.StatusBadRequest
|
||
|
case codes.Unimplemented:
|
||
|
return http.StatusNotImplemented
|
||
|
case codes.Internal:
|
||
|
return http.StatusInternalServerError
|
||
|
case codes.Unavailable:
|
||
|
return http.StatusServiceUnavailable
|
||
|
case codes.DataLoss:
|
||
|
return http.StatusInternalServerError
|
||
|
}
|
||
|
return http.StatusInternalServerError
|
||
|
}
|
||
1 year ago
|
func CodeFromGrpcError(err error) int {
|
||
|
code := status.Code(err)
|
||
|
return FromGRPCCode(code)
|
||
2 years ago
|
}
|
||
1 year ago
|
func IsGrpcError(err error) bool {
|
||
|
if err == nil {
|
||
|
return false
|
||
|
}
|
||
|
_, ok := err.(interface {
|
||
|
GRPCStatus() *status.Status
|
||
|
})
|
||
|
return ok
|
||
2 years ago
|
}
|