-
Notifications
You must be signed in to change notification settings - Fork 56
/
const.go
257 lines (249 loc) · 14.1 KB
/
const.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package gear
import "net/http"
// MIME types
const (
// Got from https://github.com/labstack/echo
MIMEApplicationJSON = "application/json"
MIMEApplicationJSONCharsetUTF8 = "application/json; charset=utf-8"
MIMEApplicationJavaScript = "application/javascript"
MIMEApplicationJavaScriptCharsetUTF8 = "application/javascript; charset=utf-8"
MIMEApplicationXML = "application/xml"
MIMEApplicationXMLCharsetUTF8 = "application/xml; charset=utf-8"
MIMEApplicationYAML = "application/yaml"
MIMEApplicationTOML = "application/toml" // https://github.com/toml-lang/toml
MIMEApplicationForm = "application/x-www-form-urlencoded"
MIMEApplicationProtobuf = "application/protobuf" // https://tools.ietf.org/html/draft-rfernando-protocol-buffers-00
MIMETextHTML = "text/html"
MIMETextHTMLCharsetUTF8 = "text/html; charset=utf-8"
MIMETextPlain = "text/plain"
MIMETextPlainCharsetUTF8 = "text/plain; charset=utf-8"
MIMEMarkdown = "text/markdown"
MIMEMarkdownCharsetUTF8 = "text/markdown; charset=utf-8"
MIMEMultipartForm = "multipart/form-data"
MIMEOctetStream = "application/octet-stream"
MIMEApplicationSchemaJSON = "application/schema+json"
MIMEApplicationSchemaInstanceJSON = "application/schema-instance+json"
MIMEApplicationSchemaJSONLD = "application/ld+json"
MIMEApplicationSchemaGraphQL = "application/graphql"
MIMEApplicationCBOR = "application/cbor"
)
// HTTP Header Fields
const (
HeaderAccept = "Accept" // Requests, Responses
HeaderAcceptCharset = "Accept-Charset" // Requests
HeaderAcceptEncoding = "Accept-Encoding" // Requests
HeaderAcceptLanguage = "Accept-Language" // Requests
HeaderAuthorization = "Authorization" // Requests
HeaderCacheControl = "Cache-Control" // Requests, Responses
HeaderContentLength = "Content-Length" // Requests, Responses
HeaderContentMD5 = "Content-MD5" // Requests, Responses
HeaderContentType = "Content-Type" // Requests, Responses
HeaderIfMatch = "If-Match" // Requests
HeaderIfModifiedSince = "If-Modified-Since" // Requests
HeaderIfNoneMatch = "If-None-Match" // Requests
HeaderIfRange = "If-Range" // Requests
HeaderIfUnmodifiedSince = "If-Unmodified-Since" // Requests
HeaderMaxForwards = "Max-Forwards" // Requests
HeaderProxyAuthorization = "Proxy-Authorization" // Requests
HeaderPragma = "Pragma" // Requests, Responses
HeaderRange = "Range" // Requests
HeaderReferer = "Referer" // Requests
HeaderUserAgent = "User-Agent" // Requests
HeaderTE = "TE" // Requests
HeaderVia = "Via" // Requests
HeaderWarning = "Warning" // Requests, Responses
HeaderCookie = "Cookie" // Requests
HeaderOrigin = "Origin" // Requests
HeaderAcceptDatetime = "Accept-Datetime" // Requests
HeaderXRequestedWith = "X-Requested-With" // Requests
HeaderXRequestID = "X-Request-Id" // Requests
HeaderXCanary = "X-Canary" // Requests, Responses
HeaderXForwardedScheme = "X-Forwarded-Scheme" // Requests
HeaderXForwardedProto = "X-Forwarded-Proto" // Requests
HeaderXForwardedFor = "X-Forwarded-For" // Requests
HeaderXForwardedHost = "X-Forwarded-Host" // Requests
HeaderXForwardedServer = "X-Forwarded-Server" // Requests
HeaderXRealIP = "X-Real-Ip" // Requests
HeaderXRealScheme = "X-Real-Scheme" // Requests
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" // Responses
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" // Responses
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" // Responses
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" // Responses
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" // Responses
HeaderAccessControlMaxAge = "Access-Control-Max-Age" // Responses
HeaderAccessControlRequestMethod = "Access-Control-Request-Method" // Responses
HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" // Responses
HeaderAcceptPatch = "Accept-Patch" // Responses
HeaderAcceptRanges = "Accept-Ranges" // Responses
HeaderAllow = "Allow" // Responses
HeaderContentEncoding = "Content-Encoding" // Responses
HeaderContentLanguage = "Content-Language" // Responses
HeaderContentLocation = "Content-Location" // Responses
HeaderContentDisposition = "Content-Disposition" // Responses
HeaderContentRange = "Content-Range" // Responses
HeaderETag = "ETag" // Responses
HeaderExpires = "Expires" // Responses
HeaderLastModified = "Last-Modified" // Responses
HeaderLink = "Link" // Responses
HeaderLocation = "Location" // Responses
HeaderP3P = "P3P" // Responses
HeaderProxyAuthenticate = "Proxy-Authenticate" // Responses
HeaderRefresh = "Refresh" // Responses
HeaderRetryAfter = "Retry-After" // Responses
HeaderServer = "Server" // Responses
HeaderSetCookie = "Set-Cookie" // Responses
HeaderStrictTransportSecurity = "Strict-Transport-Security" // Responses
HeaderTransferEncoding = "Transfer-Encoding" // Responses
HeaderUpgrade = "Upgrade" // Responses
HeaderVary = "Vary" // Responses
HeaderWWWAuthenticate = "WWW-Authenticate" // Responses
HeaderPublicKeyPins = "Public-Key-Pins" // Responses
HeaderPublicKeyPinsReportOnly = "Public-Key-Pins-Report-Only" // Responses
HeaderRefererPolicy = "Referrer-Policy" // Responses
// Common Non-Standard Response Headers
HeaderXFrameOptions = "X-Frame-Options" // Responses
HeaderXXSSProtection = "X-XSS-Protection" // Responses
HeaderContentSecurityPolicy = "Content-Security-Policy" // Responses
HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only" // Responses
HeaderXContentSecurityPolicy = "X-Content-Security-Policy" // Responses
HeaderXWebKitCSP = "X-WebKit-CSP" // Responses
HeaderXContentTypeOptions = "X-Content-Type-Options" // Responses
HeaderXPoweredBy = "X-Powered-By" // Responses
HeaderXUACompatible = "X-UA-Compatible" // Responses
HeaderXCSRFToken = "X-CSRF-Token" // Responses
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override" // Responses
HeaderXDNSPrefetchControl = "X-DNS-Prefetch-Control" // Responses
HeaderXDownloadOptions = "X-Download-Options" // Responses
)
// Predefined errors
var (
Err = &Error{Code: http.StatusInternalServerError, Err: "Error"}
// https://golang.org/pkg/net/http/#pkg-constants
ErrBadRequest = Err.WithCode(http.StatusBadRequest).WithErr("BadRequest")
ErrUnauthorized = Err.WithCode(http.StatusUnauthorized).WithErr("Unauthorized")
ErrPaymentRequired = Err.WithCode(http.StatusPaymentRequired).WithErr("PaymentRequired")
ErrForbidden = Err.WithCode(http.StatusForbidden).WithErr("Forbidden")
ErrNotFound = Err.WithCode(http.StatusNotFound).WithErr("NotFound")
ErrMethodNotAllowed = Err.WithCode(http.StatusMethodNotAllowed).WithErr("MethodNotAllowed")
ErrNotAcceptable = Err.WithCode(http.StatusNotAcceptable).WithErr("NotAcceptable")
ErrProxyAuthRequired = Err.WithCode(http.StatusProxyAuthRequired).WithErr("ProxyAuthenticationRequired")
ErrRequestTimeout = Err.WithCode(http.StatusRequestTimeout).WithErr("RequestTimeout")
ErrConflict = Err.WithCode(http.StatusConflict).WithErr("Conflict")
ErrGone = Err.WithCode(http.StatusGone).WithErr("Gone")
ErrLengthRequired = Err.WithCode(http.StatusLengthRequired).WithErr("LengthRequired")
ErrPreconditionFailed = Err.WithCode(http.StatusPreconditionFailed).WithErr("PreconditionFailed")
ErrRequestEntityTooLarge = Err.WithCode(http.StatusRequestEntityTooLarge).WithErr("RequestEntityTooLarge")
ErrRequestURITooLong = Err.WithCode(http.StatusRequestURITooLong).WithErr("RequestURITooLong")
ErrUnsupportedMediaType = Err.WithCode(http.StatusUnsupportedMediaType).WithErr("UnsupportedMediaType")
ErrRequestedRangeNotSatisfiable = Err.WithCode(http.StatusRequestedRangeNotSatisfiable).WithErr("RequestedRangeNotSatisfiable")
ErrExpectationFailed = Err.WithCode(http.StatusExpectationFailed).WithErr("ExpectationFailed")
ErrTeapot = Err.WithCode(http.StatusTeapot).WithErr("Teapot")
ErrMisdirectedRequest = Err.WithCode(421).WithErr("MisdirectedRequest")
ErrUnprocessableEntity = Err.WithCode(http.StatusUnprocessableEntity).WithErr("UnprocessableEntity")
ErrLocked = Err.WithCode(http.StatusLocked).WithErr("Locked")
ErrFailedDependency = Err.WithCode(http.StatusFailedDependency).WithErr("FailedDependency")
ErrUpgradeRequired = Err.WithCode(http.StatusUpgradeRequired).WithErr("UpgradeRequired")
ErrPreconditionRequired = Err.WithCode(http.StatusPreconditionRequired).WithErr("PreconditionRequired")
ErrTooManyRequests = Err.WithCode(http.StatusTooManyRequests).WithErr("TooManyRequests")
ErrRequestHeaderFieldsTooLarge = Err.WithCode(http.StatusRequestHeaderFieldsTooLarge).WithErr("RequestHeaderFieldsTooLarge")
ErrUnavailableForLegalReasons = Err.WithCode(http.StatusUnavailableForLegalReasons).WithErr("UnavailableForLegalReasons")
ErrClientClosedRequest = Err.WithCode(499).WithErr("ClientClosedRequest")
ErrInternalServerError = Err.WithCode(http.StatusInternalServerError).WithErr("InternalServerError")
ErrNotImplemented = Err.WithCode(http.StatusNotImplemented).WithErr("NotImplemented")
ErrBadGateway = Err.WithCode(http.StatusBadGateway).WithErr("BadGateway")
ErrServiceUnavailable = Err.WithCode(http.StatusServiceUnavailable).WithErr("ServiceUnavailable")
ErrGatewayTimeout = Err.WithCode(http.StatusGatewayTimeout).WithErr("GatewayTimeout")
ErrHTTPVersionNotSupported = Err.WithCode(http.StatusHTTPVersionNotSupported).WithErr("HTTPVersionNotSupported")
ErrVariantAlsoNegotiates = Err.WithCode(http.StatusVariantAlsoNegotiates).WithErr("VariantAlsoNegotiates")
ErrInsufficientStorage = Err.WithCode(http.StatusInsufficientStorage).WithErr("InsufficientStorage")
ErrLoopDetected = Err.WithCode(http.StatusLoopDetected).WithErr("LoopDetected")
ErrNotExtended = Err.WithCode(http.StatusNotExtended).WithErr("NotExtended")
ErrNetworkAuthenticationRequired = Err.WithCode(http.StatusNetworkAuthenticationRequired).WithErr("NetworkAuthenticationRequired")
)
// ErrByStatus returns a gear.Error by http status.
func ErrByStatus(status int) *Error {
switch status {
case 400:
return ErrBadRequest
case 401:
return ErrUnauthorized
case 402:
return ErrPaymentRequired
case 403:
return ErrForbidden
case 404:
return ErrNotFound
case 405:
return ErrMethodNotAllowed
case 406:
return ErrNotAcceptable
case 407:
return ErrProxyAuthRequired
case 408:
return ErrRequestTimeout
case 409:
return ErrConflict
case 410:
return ErrGone
case 411:
return ErrLengthRequired
case 412:
return ErrPreconditionFailed
case 413:
return ErrRequestEntityTooLarge
case 414:
return ErrRequestURITooLong
case 415:
return ErrUnsupportedMediaType
case 416:
return ErrRequestedRangeNotSatisfiable
case 417:
return ErrExpectationFailed
case 418:
return ErrTeapot
case 421:
return ErrMisdirectedRequest
case 422:
return ErrUnprocessableEntity
case 423:
return ErrLocked
case 424:
return ErrFailedDependency
case 426:
return ErrUpgradeRequired
case 428:
return ErrPreconditionRequired
case 429:
return ErrTooManyRequests
case 431:
return ErrRequestHeaderFieldsTooLarge
case 451:
return ErrUnavailableForLegalReasons
case 499:
return ErrClientClosedRequest
case 500:
return ErrInternalServerError
case 501:
return ErrNotImplemented
case 502:
return ErrBadGateway
case 503:
return ErrServiceUnavailable
case 504:
return ErrGatewayTimeout
case 505:
return ErrHTTPVersionNotSupported
case 506:
return ErrVariantAlsoNegotiates
case 507:
return ErrInsufficientStorage
case 508:
return ErrLoopDetected
case 510:
return ErrNotExtended
case 511:
return ErrNetworkAuthenticationRequired
default:
return Err.WithCode(status)
}
}