forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
293 lines (230 loc) · 7.04 KB
/
errors.go
File metadata and controls
293 lines (230 loc) · 7.04 KB
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
package cloudflare
import (
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
)
// Error messages.
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 = "account ID is empty and must be provided"
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"
errInvalidZoneIdentifer = "invalid zone identifier: %s"
)
var (
ErrMissingAccountID = errors.New("required missing account ID")
ErrMissingZoneID = errors.New("required missing zone ID")
)
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
// 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)
}
return errString + strings.Join(errMessages, ", ")
}
// 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) RayID() string {
return e.cloudflareError.RayID
}
func (e RequestError) Type() ErrorType {
return e.cloudflareError.Type
}
// 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) RayID() string {
return e.cloudflareError.RayID
}
func (e RatelimitError) Type() ErrorType {
return e.cloudflareError.Type
}
// 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) RayID() string {
return e.cloudflareError.RayID
}
func (e ServiceError) Type() ErrorType {
return e.cloudflareError.Type
}
// 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) RayID() string {
return e.cloudflareError.RayID
}
func (e AuthenticationError) Type() ErrorType {
return e.cloudflareError.Type
}
// 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) RayID() string {
return e.cloudflareError.RayID
}
func (e AuthorizationError) Type() ErrorType {
return e.cloudflareError.Type
}
// 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) RayID() string {
return e.cloudflareError.RayID
}
func (e NotFoundError) Type() ErrorType {
return e.cloudflareError.Type
}
// 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
}