-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
114 lines (101 loc) · 3.19 KB
/
status.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package status
import (
"net/http"
gcodes "google.golang.org/grpc/codes"
)
const (
// ClientClosed is non-standard http status code,
// which defined by nginx.
// https://httpstatus.in/499/
ClientClosed = 499
)
// Converter is a status converter.
type Converter interface {
// ToGRPCCode converts an HTTP error code into the corresponding gRPC response status.
ToGRPCCode(code int) gcodes.Code
// FromGRPCCode converts a gRPC error code into the corresponding HTTP response status.
FromGRPCCode(code gcodes.Code) int
}
type statusConverter struct{}
// DefaultConverter default converter.
var DefaultConverter Converter = statusConverter{}
// ToGRPCCode converts a HTTP error code into the corresponding gRPC response status.
// See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
func (c statusConverter) ToGRPCCode(code int) gcodes.Code {
switch code {
case http.StatusOK:
return gcodes.OK
case http.StatusBadRequest:
return gcodes.InvalidArgument
case http.StatusUnauthorized:
return gcodes.Unauthenticated
case http.StatusForbidden:
return gcodes.PermissionDenied
case http.StatusNotFound:
return gcodes.NotFound
case http.StatusConflict:
return gcodes.Aborted
case http.StatusTooManyRequests:
return gcodes.ResourceExhausted
case http.StatusInternalServerError:
return gcodes.Internal
case http.StatusNotImplemented:
return gcodes.Unimplemented
case http.StatusServiceUnavailable:
return gcodes.Unavailable
case http.StatusGatewayTimeout:
return gcodes.DeadlineExceeded
case ClientClosed:
return gcodes.Canceled
}
return gcodes.Unknown
}
// FromGRPCCode converts a gRPC error code into the corresponding HTTP response status.
// See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
func (c statusConverter) FromGRPCCode(code gcodes.Code) int {
switch code {
case gcodes.OK:
return http.StatusOK
case gcodes.Canceled:
return ClientClosed
case gcodes.Unknown:
return http.StatusInternalServerError
case gcodes.InvalidArgument:
return http.StatusBadRequest
case gcodes.DeadlineExceeded:
return http.StatusGatewayTimeout
case gcodes.NotFound:
return http.StatusNotFound
case gcodes.AlreadyExists:
return http.StatusConflict
case gcodes.PermissionDenied:
return http.StatusForbidden
case gcodes.Unauthenticated:
return http.StatusUnauthorized
case gcodes.ResourceExhausted:
return http.StatusTooManyRequests
case gcodes.FailedPrecondition:
return http.StatusBadRequest
case gcodes.Aborted:
return http.StatusConflict
case gcodes.OutOfRange:
return http.StatusBadRequest
case gcodes.Unimplemented:
return http.StatusNotImplemented
case gcodes.Internal:
return http.StatusInternalServerError
case gcodes.Unavailable:
return http.StatusServiceUnavailable
case gcodes.DataLoss:
return http.StatusInternalServerError
}
return http.StatusInternalServerError
}
// ToGRPCCode converts an HTTP error code into the corresponding gRPC response status.
func ToGRPCCode(code int) gcodes.Code {
return DefaultConverter.ToGRPCCode(code)
}
// FromGRPCCode converts a gRPC error code into the corresponding HTTP response status.
func FromGRPCCode(code gcodes.Code) int {
return DefaultConverter.FromGRPCCode(code)
}