-
Notifications
You must be signed in to change notification settings - Fork 13
/
codes.go
40 lines (37 loc) · 995 Bytes
/
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
package grpchttp
import (
"net/http"
"google.golang.org/grpc/codes"
)
// CodeFromHTTPStatus converts an HTTP response status into the corresponding
// gRPC error code.
func CodeFromHTTPStatus(status int) codes.Code {
switch status {
case http.StatusOK:
return codes.OK
case http.StatusTooManyRequests:
return codes.ResourceExhausted
case http.StatusRequestTimeout:
return codes.DeadlineExceeded
case http.StatusInternalServerError:
return codes.Unknown
case http.StatusBadRequest:
return codes.InvalidArgument
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.StatusServiceUnavailable:
return codes.Unavailable
default:
return codes.Unknown
}
}