forked from cloudflare/cloudflare-go
/
errors.go
386 lines (306 loc) · 10.1 KB
/
errors.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package cloudflare
import (
"errors"
"fmt"
"net/http"
"strings"
)
const (
errEmptyCredentials = "invalid credentials: key & email must not be empty" //nolint:gosec,unused
errEmptyAPIToken = "invalid credentials: API Token must not be empty" //nolint:gosec,unused
errInternalServiceError = "internal service error"
errMakeRequestError = "error from makeRequest"
errUnmarshalError = "error unmarshalling the JSON response"
errUnmarshalErrorBody = "error unmarshalling the JSON response error body"
errRequestNotSuccessful = "error reported by API"
errMissingAccountID = "required missing account ID"
errMissingZoneID = "required missing zone ID"
errMissingAccountOrZoneID = "either account ID or zone ID must be provided"
errAccountIDAndZoneIDAreMutuallyExclusive = "account ID and zone ID are mutually exclusive"
errMissingResourceIdentifier = "required missing resource identifier"
errOperationStillRunning = "bulk operation did not finish before timeout"
errOperationUnexpectedStatus = "bulk operation returned an unexpected status"
errResultInfo = "incorrect pagination info (result_info) in responses"
errManualPagination = "unexpected pagination options passed to functions that handle pagination automatically"
errInvalidResourceIdentifer = "invalid resource identifier: %s"
errInvalidZoneIdentifer = "invalid zone identifier: %s"
errAPIKeysAndTokensAreMutuallyExclusive = "API keys and tokens are mutually exclusive" //nolint:gosec
errMissingCredentials = "no credentials provided"
errInvalidResourceContainerAccess = "requested resource container (%q) is not supported for this endpoint"
)
var (
ErrAPIKeysAndTokensAreMutuallyExclusive = errors.New(errAPIKeysAndTokensAreMutuallyExclusive)
ErrMissingCredentials = errors.New(errMissingCredentials)
ErrMissingAccountID = errors.New(errMissingAccountID)
ErrMissingZoneID = errors.New(errMissingZoneID)
ErrAccountIDOrZoneIDAreRequired = errors.New(errMissingAccountOrZoneID)
ErrAccountIDAndZoneIDAreMutuallyExclusive = errors.New(errAccountIDAndZoneIDAreMutuallyExclusive)
ErrMissingResourceIdentifier = errors.New(errMissingResourceIdentifier)
)
type ErrorType string
const (
ErrorTypeRequest ErrorType = "request"
ErrorTypeAuthentication ErrorType = "authentication"
ErrorTypeAuthorization ErrorType = "authorization"
ErrorTypeNotFound ErrorType = "not_found"
ErrorTypeRateLimit ErrorType = "rate_limit"
ErrorTypeService ErrorType = "service"
)
type Error struct {
// The classification of error encountered.
Type ErrorType
// StatusCode is the HTTP status code from the response.
StatusCode int
// Errors is all of the error messages and codes, combined.
Errors []ResponseInfo
// ErrorCodes is a list of all the error codes.
ErrorCodes []int
// ErrorMessages is a list of all the error codes.
ErrorMessages []string
// Messages is a list of informational messages provided by the endpoint.
Messages []ResponseInfo
// RayID is the internal identifier for the request that was made.
RayID string
}
func (e Error) Error() string {
var errString string
errMessages := []string{}
for _, err := range e.Errors {
m := ""
if err.Message != "" {
m += err.Message
}
if err.Code != 0 {
m += fmt.Sprintf(" (%d)", err.Code)
}
errMessages = append(errMessages, m)
}
msgs := []string{}
for _, m := range e.Messages {
msgs = append(msgs, m.Message)
}
errString += strings.Join(errMessages, ", ")
// `Messages` is primarily used for additional validation failure notes in
// page rules. This shouldn't be used going forward but instead, use the
// error fields appropriately.
if len(msgs) > 0 {
errString += "\n" + strings.Join(msgs, " \n")
}
return errString
}
// RequestError is for 4xx errors that we encounter not covered elsewhere
// (generally bad payloads).
type RequestError struct {
cloudflareError *Error
}
func (e RequestError) Error() string {
return e.cloudflareError.Error()
}
func (e RequestError) Errors() []ResponseInfo {
return e.cloudflareError.Errors
}
func (e RequestError) ErrorCodes() []int {
return e.cloudflareError.ErrorCodes
}
func (e RequestError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}
func (e RequestError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}
func (e RequestError) Messages() []ResponseInfo {
return e.cloudflareError.Messages
}
func (e RequestError) RayID() string {
return e.cloudflareError.RayID
}
func (e RequestError) Type() ErrorType {
return e.cloudflareError.Type
}
func NewRequestError(e *Error) RequestError {
return RequestError{
cloudflareError: e,
}
}
// RatelimitError is for HTTP 429s where the service is telling the client to
// slow down.
type RatelimitError struct {
cloudflareError *Error
}
func (e RatelimitError) Error() string {
return e.cloudflareError.Error()
}
func (e RatelimitError) Errors() []ResponseInfo {
return e.cloudflareError.Errors
}
func (e RatelimitError) ErrorCodes() []int {
return e.cloudflareError.ErrorCodes
}
func (e RatelimitError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}
func (e RatelimitError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}
func (e RatelimitError) RayID() string {
return e.cloudflareError.RayID
}
func (e RatelimitError) Type() ErrorType {
return e.cloudflareError.Type
}
func NewRatelimitError(e *Error) RatelimitError {
return RatelimitError{
cloudflareError: e,
}
}
// ServiceError is a handler for 5xx errors returned to the client.
type ServiceError struct {
cloudflareError *Error
}
func (e ServiceError) Error() string {
return e.cloudflareError.Error()
}
func (e ServiceError) Errors() []ResponseInfo {
return e.cloudflareError.Errors
}
func (e ServiceError) ErrorCodes() []int {
return e.cloudflareError.ErrorCodes
}
func (e ServiceError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}
func (e ServiceError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}
func (e ServiceError) RayID() string {
return e.cloudflareError.RayID
}
func (e ServiceError) Type() ErrorType {
return e.cloudflareError.Type
}
func NewServiceError(e *Error) ServiceError {
return ServiceError{
cloudflareError: e,
}
}
// AuthenticationError is for HTTP 401 responses.
type AuthenticationError struct {
cloudflareError *Error
}
func (e AuthenticationError) Error() string {
return e.cloudflareError.Error()
}
func (e AuthenticationError) Errors() []ResponseInfo {
return e.cloudflareError.Errors
}
func (e AuthenticationError) ErrorCodes() []int {
return e.cloudflareError.ErrorCodes
}
func (e AuthenticationError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}
func (e AuthenticationError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}
func (e AuthenticationError) RayID() string {
return e.cloudflareError.RayID
}
func (e AuthenticationError) Type() ErrorType {
return e.cloudflareError.Type
}
func NewAuthenticationError(e *Error) AuthenticationError {
return AuthenticationError{
cloudflareError: e,
}
}
// AuthorizationError is for HTTP 403 responses.
type AuthorizationError struct {
cloudflareError *Error
}
func (e AuthorizationError) Error() string {
return e.cloudflareError.Error()
}
func (e AuthorizationError) Errors() []ResponseInfo {
return e.cloudflareError.Errors
}
func (e AuthorizationError) ErrorCodes() []int {
return e.cloudflareError.ErrorCodes
}
func (e AuthorizationError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}
func (e AuthorizationError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}
func (e AuthorizationError) RayID() string {
return e.cloudflareError.RayID
}
func (e AuthorizationError) Type() ErrorType {
return e.cloudflareError.Type
}
func NewAuthorizationError(e *Error) AuthorizationError {
return AuthorizationError{
cloudflareError: e,
}
}
// NotFoundError is for HTTP 404 responses.
type NotFoundError struct {
cloudflareError *Error
}
func (e NotFoundError) Error() string {
return e.cloudflareError.Error()
}
func (e NotFoundError) Errors() []ResponseInfo {
return e.cloudflareError.Errors
}
func (e NotFoundError) ErrorCodes() []int {
return e.cloudflareError.ErrorCodes
}
func (e NotFoundError) ErrorMessages() []string {
return e.cloudflareError.ErrorMessages
}
func (e NotFoundError) InternalErrorCodeIs(code int) bool {
return e.cloudflareError.InternalErrorCodeIs(code)
}
func (e NotFoundError) RayID() string {
return e.cloudflareError.RayID
}
func (e NotFoundError) Type() ErrorType {
return e.cloudflareError.Type
}
func NewNotFoundError(e *Error) NotFoundError {
return NotFoundError{
cloudflareError: e,
}
}
// ClientError returns a boolean whether or not the raised error was caused by
// something client side.
func (e *Error) ClientError() bool {
return e.StatusCode >= http.StatusBadRequest &&
e.StatusCode < http.StatusInternalServerError
}
// ClientRateLimited returns a boolean whether or not the raised error was
// caused by too many requests from the client.
func (e *Error) ClientRateLimited() bool {
return e.Type == ErrorTypeRateLimit
}
// InternalErrorCodeIs returns a boolean whether or not the desired internal
// error code is present in `e.InternalErrorCodes`.
func (e *Error) InternalErrorCodeIs(code int) bool {
for _, errCode := range e.ErrorCodes {
if errCode == code {
return true
}
}
return false
}
// ErrorMessageContains returns a boolean whether or not a substring exists in
// any of the `e.ErrorMessages` slice entries.
func (e *Error) ErrorMessageContains(s string) bool {
for _, errMsg := range e.ErrorMessages {
if strings.Contains(errMsg, s) {
return true
}
}
return false
}