/
errors.go
292 lines (224 loc) · 11.4 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
package fastly
import (
"bytes"
"errors"
"fmt"
"net/http"
"github.com/google/jsonapi"
)
// ErrMissingServiceID is an error that is returned when an input struct requires
// a "ServiceID" key, but one was not set.
var ErrMissingServiceID = errors.New("missing required field 'ServiceID'")
// ErrMissingStatus is an error that is returned when an input struct requires
// a "Status" key, but one was not set.
var ErrMissingStatus = errors.New("missing required field 'Status'")
// ErrMissingTag is an error that is returned when an input struct requires
// a "Tag" key, but one was not set.
var ErrMissingTag = errors.New("missing required field 'Tag'")
// ErrMissingServiceVersion is an error that is returned when an input struct requires
// a "ServiceVersion" key, but one was not set.
var ErrMissingServiceVersion = errors.New("missing required field 'ServiceVersion'")
// ErrMissingContent is an error that is returned when an input struct requires a
// "Content" key, but one was not set.
var ErrMissingContent = errors.New("missing required field 'Content'")
// ErrMissingLogin is an error that is returned when an input struct requires a
// "Login" key, but one was not set.
var ErrMissingLogin = errors.New("missing required field 'Login'")
// ErrMissingName is an error that is returned when an input struct requires a
// "Name" key, but one was not set.
var ErrMissingName = errors.New("missing required field 'Name'")
// ErrMissingKey is an error that is returned when an input struct requires a
// "Key" key, but one was not set.
var ErrMissingKey = errors.New("missing required field 'Key'")
// ErrMissingURL is an error that is returned when an input struct requires a
// "URL" key, but one was not set.
var ErrMissingURL = errors.New("missing required field 'URL'")
// ErrMissingID is an error that is returned when an input struct requires an
// "ID" key, but one was not set.
var ErrMissingID = errors.New("missing required field 'ID'")
// ErrMissingDictionary is an error that is returned when an input struct
// requires a "Dictionary" key, but one was not set.
var ErrMissingDictionary = errors.New("missing required field 'Dictionary'")
// ErrMissingItemKey is an error that is returned when an input struct
// requires a "ItemKey" key, but one was not set.
var ErrMissingItemKey = errors.New("missing required field 'ItemKey'")
// ErrMissingFrom is an error that is returned when an input struct
// requires a "From" key, but one was not set.
var ErrMissingFrom = errors.New("missing required field 'From'")
// ErrMissingTo is an error that is returned when an input struct
// requires a "To" key, but one was not set.
var ErrMissingTo = errors.New("missing required field 'To'")
// ErrMissingDirector is an error that is returned when an input struct
// requires a "From" key, but one was not set.
var ErrMissingDirector = errors.New("missing required field 'Director'")
// ErrMissingBackend is an error that is returned when an input struct
// requires a "Backend" key, but one was not set.
var ErrMissingBackend = errors.New("missing required field 'Backend'")
// ErrMissingYear is an error that is returned when an input struct
// requires a "Year" key, but one was not set.
var ErrMissingYear = errors.New("missing required field 'Year'")
// ErrMissingMonth is an error that is returned when an input struct
// requires a "Month" key, but one was not set.
var ErrMissingMonth = errors.New("missing required field 'Month'")
// ErrMissingNewName is an erorr that is returned when an input struct
// requires a "NewName" key, but one was not set
var ErrMissingNewName = errors.New("missing required field 'NewName'")
// ErrMissingACLID is an error that is returned when an input struct
// required an "ACLID" key, but one is not set
var ErrMissingACLID = errors.New("missing required field 'ACLID'")
// ErrMissingIP is an error that is returned when an input struct
// required an "IP" key, but one is not set
var ErrMissingIP = errors.New("missing required field 'IP'")
// ErrMissingCustomerID is an error that is returned was an input struct
// requires a "CustomerID" key, but one was not set
var ErrMissingCustomerID = errors.New("missing required field 'CustomerID'")
// ErrMissingEventID is an error that is returned was an input struct
// requires a "EventID" key, but one was not set
var ErrMissingEventID = errors.New("missing required field 'EventID'")
// ErrMissingWAFID is an error that is returned when an input struct
// requires a "WAFID" key, but one was not set.
var ErrMissingWAFID = errors.New("missing required field 'WAFID'")
// ErrMissingWAFVersionNumber is an error that is returned when an input struct
// requires a "WAFVersionNumber" key, but one was not set.
var ErrMissingWAFVersionNumber = errors.New("missing required field 'WAFVersionNumber'")
// ErrMissingWAFVersionID is an error that is returned when an input struct
// requires a "WAFVersionID" key, but one was not set.
var ErrMissingWAFVersionID = errors.New("missing required field 'WAFVersionID'")
// ErrMissingWAFActiveRuleList is an error that is returned when an input struct
// requires a list of WAF active rules, but it is empty.
var ErrMissingWAFActiveRuleList = errors.New("WAF active rules slice is empty")
// ErrMissingWAFRuleExclusionNumber is an error that is returned when an input struct
// requires a "WAFExclusionNumber" key, but one was not set.
var ErrMissingWAFRuleExclusionNumber = errors.New("missing required field 'WAFExclusionNumber'")
// ErrMissingWAFRuleExclusion is an error that is returned when an input struct
// requires a "WAFRuleExclusion" key, but one was not set.
var ErrMissingWAFRuleExclusion = errors.New("missing required field 'WAFRuleExclusion'")
// ErrMissingOWASPID is an error that is returned was an input struct
// requires a "OWASPID" key, but one was not set
var ErrMissingOWASPID = errors.New("missing required field 'OWASPID'")
// ErrMissingRuleID is an error that is returned was an input struct
// requires a "RuleID" key, but one was not set
var ErrMissingRuleID = errors.New("missing required field 'RuleID'")
// ErrMissingConfigSetID is an error that is returned was an input struct
// requires a "ConfigSetID" key, but one was not set
var ErrMissingConfigSetID = errors.New("missing required field 'ConfigSetID'")
// ErrMissingWAFList is an error that is returned was an input struct
// requires a list of WAF id's, but it is empty
var ErrMissingWAFList = errors.New("WAF slice is empty")
// ErrMissingPool is an error that is returned when an input struct requires
// a "Pool" key, but one was not set.
var ErrMissingPool = errors.New("missing required field 'Pool'")
// ErrMissingServer is an error that is returned when an input struct requires
// a "Server" key, but one was not set.
var ErrMissingServer = errors.New("missing required field 'Server'")
// ErrMissingAddress is an error that is returned when an input struct requires
// a "Address" key, but one was not set.
var ErrMissingAddress = errors.New("missing required field 'Address'")
// ErrBatchUpdateMaximumItemsExceeded is an error that indicates that too many batch operations are being executed.
// The Fastly API specifies an maximum limit.
var ErrBatchUpdateMaximumOperationsExceeded = errors.New("batch modify maximum operations exceeded")
// ErrMissingKMSKeyID is an error that is returned from an input struct that requires
// a "ServerSideEncryptionKMSKeyID" key, but one was not set.
var ErrMissingKMSKeyID = errors.New("missing required field 'ServerSideEncryptionKMSKeyID'")
// ErrMissingCertBlob is an error that is returned from an input struct that requires
// a "CertBlob" key, but one was not set.
var ErrMissingCertBlob = errors.New("missing required field 'CertBlob'")
// ErrMissingIntermediatesBlob is an error that is returned from an input struct that requires
// a "IntermediatesBlob" key, but one was not set.
var ErrMissingIntermediatesBlob = errors.New("missing required field 'IntermediatesBlob'")
// ErrMissingTLSCertificate is an error that is returned from an input struct that requires
// a "Certificate" field assigned a "TLSCertificate" struct, but one was not set.
var ErrMissingTLSCertificate = errors.New("missing required field 'Certificate'")
// ErrMissingTLSConfiguration is an error that is returned from an input struct that requires
// a "Configuration" field assigned a "TLSConfiguration" struct, but one was not set.
var ErrMissingTLSConfiguration = errors.New("missing required field 'Configuration'")
// ErrMissingTLSDomain is an error that is returned from an input struct that requires
// a "Domain" field assigned a "TLSDomain" struct, but one was not set.
var ErrMissingTLSDomain = errors.New("missing required field 'Domain'")
// ErrStatusNotOk is an error that indicates that indicates that the response body returned
// by the Fastly API was not `{"status": "ok"}`
var ErrStatusNotOk = errors.New("unexpected 'status' field in API response body")
// ErrNotOK is a generic error indicating that something is not okay.
var ErrNotOK = errors.New("not ok")
// Ensure HTTPError is, in fact, an error.
var _ error = (*HTTPError)(nil)
// HTTPError is a custom error type that wraps an HTTP status code with some
// helper functions.
type HTTPError struct {
// StatusCode is the HTTP status code (2xx-5xx).
StatusCode int
Errors []*ErrorObject `mapstructure:"errors"`
}
// ErrorObject is a single error.
type ErrorObject struct {
ID string `mapstructure:"id"`
Title string `mapstructure:"title"`
Detail string `mapstructure:"detail"`
Status string `mapstructure:"status"`
Code string `mapstructure:"code"`
Meta *map[string]interface{} `mapstructure:"meta"`
}
// legacyError represents the older-style errors from Fastly. It is private
// because it is automatically converted to a jsonapi error.
type legacyError struct {
Message string `mapstructure:"msg"`
Detail string `mapstructure:"detail"`
}
// NewHTTPError creates a new HTTP error from the given code.
func NewHTTPError(resp *http.Response) *HTTPError {
var e HTTPError
e.StatusCode = resp.StatusCode
if resp.Body == nil {
return &e
}
// If this is a jsonapi response, decode it accordingly
if resp.Header.Get("Content-Type") == jsonapi.MediaType {
if err := decodeBodyMap(resp.Body, &e); err != nil {
panic(err)
}
} else {
var lerr *legacyError
decodeBodyMap(resp.Body, &lerr)
if lerr != nil {
e.Errors = append(e.Errors, &ErrorObject{
Title: lerr.Message,
Detail: lerr.Detail,
})
}
}
return &e
}
// Error implements the error interface and returns the string representing the
// error text that includes the status code and the corresponding status text.
func (e *HTTPError) Error() string {
var b bytes.Buffer
fmt.Fprintf(&b, "%d - %s:", e.StatusCode, http.StatusText(e.StatusCode))
for _, e := range e.Errors {
fmt.Fprintf(&b, "\n")
if e.ID != "" {
fmt.Fprintf(&b, "\n ID: %s", e.ID)
}
if e.Title != "" {
fmt.Fprintf(&b, "\n Title: %s", e.Title)
}
if e.Detail != "" {
fmt.Fprintf(&b, "\n Detail: %s", e.Detail)
}
if e.Code != "" {
fmt.Fprintf(&b, "\n Code: %s", e.Code)
}
if e.Meta != nil {
fmt.Fprintf(&b, "\n Meta: %v", *e.Meta)
}
}
return b.String()
}
// String implements the stringer interface and returns the string representing
// the string text that includes the status code and corresponding status text.
func (e *HTTPError) String() string {
return e.Error()
}
// IsNotFound returns true if the HTTP error code is a 404, false otherwise.
func (e *HTTPError) IsNotFound() bool {
return e.StatusCode == 404
}