-
Notifications
You must be signed in to change notification settings - Fork 3
/
codes.go
53 lines (49 loc) · 1.39 KB
/
codes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package grpctools
import (
"net/http"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// HTTPStatus returns a http stats from a grpc error code
func HTTPStatus(c codes.Code) int {
switch c {
case codes.OK:
return http.StatusOK
case codes.Canceled:
return http.StatusGone
case codes.Unknown:
return http.StatusInternalServerError
case codes.InvalidArgument:
return http.StatusBadRequest
case codes.DeadlineExceeded:
return http.StatusRequestTimeout
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.StatusPreconditionFailed
case codes.Aborted:
return http.StatusPreconditionFailed
case codes.OutOfRange:
return http.StatusRequestedRangeNotSatisfiable
case codes.Unimplemented:
return http.StatusNotImplemented
case codes.Internal:
return http.StatusInternalServerError
case codes.Unavailable:
return http.StatusServiceUnavailable
case codes.DataLoss:
return http.StatusInternalServerError
default:
return http.StatusTeapot
}
}
// HTTPStatusFromError returns a http stats from a grpc error code
func HTTPStatusFromError(err error) int { return HTTPStatus(status.Code(err)) }