-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalerrors.go
113 lines (106 loc) · 3.82 KB
/
globalerrors.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
package globalerrors
import (
"errors"
"google.golang.org/grpc/codes"
)
func HTTPStatus(customError error) int {
return status[int](customError, httpStatusCodesMap)
}
func GRPCStatus(customError error) codes.Code {
return status[codes.Code](customError, grpcStatusCodesMap)
}
func status[T any](customError error, m map[error]T) T {
for _, e := range globalErrors {
if errors.Is(customError, e) {
s, exists := m[e]
if exists {
return s
}
}
}
return m[InternalServerError]
}
var globalErrors = []error{
BadRequest,
Unauthorized,
PaymentRequired,
Forbidden,
NotFound,
MethodNotAllowed,
NotAcceptable,
ProxyAuthRequired,
RequestTimeout,
Conflict,
Gone,
LengthRequired,
PreconditionFailed,
RequestEntityTooLarge,
RequestURITooLong,
UnsupportedMediaType,
RequestedRangeNotSatisfiable,
ExpectationFailed,
Teapot,
MisdirectedRequest,
UnprocessableEntity,
Locked,
FailedDependency,
TooEarly,
UpgradeRequired,
PreconditionRequired,
TooManyRequests,
RequestHeaderFieldsTooLarge,
UnavailableForLegalReasons,
InternalServerError,
NotImplemented,
BadGateway,
ServiceUnavailable,
GatewayTimeout,
HTTPVersionNotSupported,
VariantAlsoNegotiates,
InsufficientStorage,
LoopDetected,
NotExtended,
NetworkAuthenticationRequired,
}
var (
BadRequest = errors.New("bad request")
Unauthorized = errors.New("unauthorized")
PaymentRequired = errors.New("payment required")
Forbidden = errors.New("forbidden")
NotFound = errors.New("not found")
MethodNotAllowed = errors.New("method not allowed")
NotAcceptable = errors.New("not acceptable")
ProxyAuthRequired = errors.New("proxy authentication required")
RequestTimeout = errors.New("request timeout")
Conflict = errors.New("conflict")
Gone = errors.New("gone")
LengthRequired = errors.New("length required")
PreconditionFailed = errors.New("precondition failed")
RequestEntityTooLarge = errors.New("request entity too large")
RequestURITooLong = errors.New("request URI too long")
UnsupportedMediaType = errors.New("unsupported media type")
RequestedRangeNotSatisfiable = errors.New("requested range not satisfiable")
ExpectationFailed = errors.New("expectation failed")
Teapot = errors.New("I'm a teapot")
MisdirectedRequest = errors.New("misdirected request")
UnprocessableEntity = errors.New("unprocessable entity")
Locked = errors.New("locked")
FailedDependency = errors.New("failed dependency")
TooEarly = errors.New("too early")
UpgradeRequired = errors.New("upgrade required")
PreconditionRequired = errors.New("precondition required")
TooManyRequests = errors.New("too many requests")
RequestHeaderFieldsTooLarge = errors.New("request header fields too large")
UnavailableForLegalReasons = errors.New("unavailable for legal reasons")
InternalServerError = errors.New("internal server error")
NotImplemented = errors.New("not implemented")
BadGateway = errors.New("bad gateway")
ServiceUnavailable = errors.New("service unavailable")
GatewayTimeout = errors.New("gateway timeout")
HTTPVersionNotSupported = errors.New("HTTP version not supported")
VariantAlsoNegotiates = errors.New("variant also negotiates")
InsufficientStorage = errors.New("insufficient storage")
LoopDetected = errors.New("loop detected")
NotExtended = errors.New("not extended")
NetworkAuthenticationRequired = errors.New("network authentication required")
)