forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
125 lines (103 loc) · 3.55 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
package cloudflare
import (
"fmt"
"net/http"
"strings"
)
// 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
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"
)
// APIRequestError is a type of error raised by API calls made by this library.
type APIRequestError struct {
StatusCode int
Errors []ResponseInfo
}
func (e APIRequestError) Error() string {
errString := ""
errString += fmt.Sprintf("HTTP status %d", e.StatusCode)
if len(e.Errors) > 0 {
errString += ": "
}
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, ", ")
}
// HTTPStatusCode exposes the HTTP status from the error response encountered.
func (e APIRequestError) HTTPStatusCode() int {
return e.StatusCode
}
// ErrorMessages exposes the error messages as a slice of strings from the error
// response encountered.
func (e *APIRequestError) ErrorMessages() []string {
messages := []string{}
for _, e := range e.Errors {
messages = append(messages, e.Message)
}
return messages
}
// InternalErrorCodes exposes the internal error codes as a slice of int from
// the error response encountered.
func (e *APIRequestError) InternalErrorCodes() []int {
ec := []int{}
for _, e := range e.Errors {
ec = append(ec, e.Code)
}
return ec
}
// ServiceError returns a boolean whether or not the raised error was caused by
// an internal service.
func (e *APIRequestError) ServiceError() bool {
return e.StatusCode >= http.StatusInternalServerError &&
e.StatusCode < 600
}
// ClientError returns a boolean whether or not the raised error was caused by
// something client side.
func (e *APIRequestError) 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 *APIRequestError) ClientRateLimited() bool {
return e.StatusCode == http.StatusTooManyRequests
}
// InternalErrorCodeIs returns a boolean whether or not the desired internal
// error code is present in `e.InternalErrorCodes`.
func (e *APIRequestError) InternalErrorCodeIs(code int) bool {
for _, errCode := range e.InternalErrorCodes() {
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 *APIRequestError) ErrorMessageContains(s string) bool {
for _, errMsg := range e.ErrorMessages() {
if strings.Contains(errMsg, s) {
return true
}
}
return false
}