-
Notifications
You must be signed in to change notification settings - Fork 82
/
errors_test.go
518 lines (456 loc) · 13.9 KB
/
errors_test.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package linodego
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-resty/resty/v2"
"github.com/google/go-cmp/cmp"
)
type testStringer string
func (t testStringer) String() string {
return string(t)
}
type testError string
func (e testError) Error() string {
return string(e)
}
func restyError(reason, field string) *resty.Response {
var reasons []APIErrorReason
// allow for an empty reasons
if reason != "" && field != "" {
reasons = append(reasons, APIErrorReason{
Reason: reason,
Field: field,
})
}
return &resty.Response{
RawResponse: &http.Response{
StatusCode: 500,
},
Request: &resty.Request{
Error: &APIError{
Errors: reasons,
},
},
}
}
func TestNewError(t *testing.T) {
if NewError(nil) != nil {
t.Errorf("nil error should return nil")
}
if NewError(struct{}{}).Code != ErrorUnsupported {
t.Error("empty struct should return unsupported error type")
}
err := errors.New("test")
newErr := NewError(err)
if newErr.Message != err.Error() && newErr.Code != ErrorFromError {
t.Error("error should return ErrorFromError")
}
if newErr.Error() != "[002] test" {
t.Error("Error should support Error() formatter with code")
}
if NewError(newErr) != newErr {
t.Error("Error should be itself")
}
if err := NewError(&resty.Response{Request: &resty.Request{}}); err.Message != "Unexpected Resty Error Response, no error" {
t.Error("Unexpected Resty Error Response, no error")
}
if err := NewError(restyError("testreason", "testfield")); err.Message != "[testfield] testreason" {
t.Error("rest response error should should be set")
}
if err := NewError("stringerror"); err.Message != "stringerror" || err.Code != ErrorFromString {
t.Errorf("string error should be set")
}
if err := NewError(testStringer("teststringer")); err.Message != "teststringer" || err.Code != ErrorFromStringer {
t.Errorf("error should be set for a stringer interface")
}
if err := NewError(testError("testerror")); err.Message != "testerror" || err.Code != ErrorFromError {
t.Errorf("error should be set for an error interface")
}
}
func createTestServer(method, route, contentType, body string, statusCode int) (*httptest.Server, *Client) {
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Method == method && r.URL.Path == route {
rw.Header().Add("Content-Type", contentType)
rw.WriteHeader(statusCode)
rw.Write([]byte(body))
return
}
rw.WriteHeader(http.StatusNotImplemented)
})
ts := httptest.NewServer(h)
client := NewClient(nil)
client.SetBaseURL(ts.URL)
return ts, &client
}
func TestCoupleAPIErrors(t *testing.T) {
t.Run("not nil error generates error", func(t *testing.T) {
err := errors.New("test")
if _, err := coupleAPIErrors(nil, err); !cmp.Equal(err, NewError(err)) {
t.Errorf("expect a not nil error to be returned as an Error")
}
})
t.Run("resty 500 response error with reasons", func(t *testing.T) {
if _, err := coupleAPIErrors(restyError("testreason", "testfield"), nil); err.Error() != "[500] [testfield] testreason" {
t.Error("resty error should return with proper format [code] [field] reason")
}
})
t.Run("resty 500 response error without reasons", func(t *testing.T) {
if _, err := coupleAPIErrors(restyError("", ""), nil); err != nil {
t.Error("resty error with no reasons should return no error")
}
})
t.Run("resty response with nil error", func(t *testing.T) {
emptyErr := &resty.Response{
RawResponse: &http.Response{
StatusCode: 500,
},
Request: &resty.Request{
Error: nil,
},
}
if _, err := coupleAPIErrors(emptyErr, nil); err != nil {
t.Error("resty error with no reasons should return no error")
}
})
t.Run("generic html error", func(t *testing.T) {
rawResponse := `<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx</center>
</body>
</html>`
route := "/v4/linode/instances/123"
ts, client := createTestServer(http.MethodGet, route, "text/html", rawResponse, http.StatusInternalServerError)
// client.SetDebug(true)
defer ts.Close()
expectedError := Error{
Code: http.StatusInternalServerError,
Message: "Unexpected Content-Type: Expected: application/json, Received: text/html\nResponse body: " + rawResponse,
}
_, err := coupleAPIErrors(client.R(context.Background()).SetResult(&Instance{}).Get(ts.URL + route))
if diff := cmp.Diff(expectedError, err); diff != "" {
t.Errorf("expected error to match but got diff:\n%s", diff)
}
})
t.Run("bad gateway error", func(t *testing.T) {
rawResponse := []byte(`<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>`)
buf := io.NopCloser(bytes.NewBuffer(rawResponse))
resp := &resty.Response{
Request: &resty.Request{
Error: errors.New("Bad Gateway"),
},
RawResponse: &http.Response{
Header: http.Header{
"Content-Type": []string{"text/html"},
},
StatusCode: http.StatusBadGateway,
Body: buf,
},
}
expectedError := Error{
Code: http.StatusBadGateway,
Message: http.StatusText(http.StatusBadGateway),
}
if _, err := coupleAPIErrors(resp, nil); !cmp.Equal(err, expectedError) {
t.Errorf("expected error %#v to match error %#v", err, expectedError)
}
})
}
func TestCoupleAPIErrorsHTTP(t *testing.T) {
t.Run("not nil error generates error", func(t *testing.T) {
err := errors.New("test")
if _, err := coupleAPIErrorsHTTP(nil, err); !cmp.Equal(err, NewError(err)) {
t.Errorf("expect a not nil error to be returned as an Error")
}
})
t.Run("http 500 response error with reasons", func(t *testing.T) {
// Create the simulated HTTP response with a 500 status and a JSON body containing the error details
apiError := APIError{
Errors: []APIErrorReason{
{Reason: "testreason", Field: "testfield"},
},
}
apiErrorBody, _ := json.Marshal(apiError)
bodyReader := io.NopCloser(bytes.NewBuffer(apiErrorBody))
resp := &http.Response{
StatusCode: http.StatusInternalServerError,
Body: bodyReader,
Header: http.Header{"Content-Type": []string{"application/json"}},
Request: &http.Request{Header: http.Header{"Accept": []string{"application/json"}}},
}
_, err := coupleAPIErrorsHTTP(resp, nil)
expectedMessage := "[500] [testfield] testreason"
if err == nil || err.Error() != expectedMessage {
t.Errorf("expected error message %q, got: %v", expectedMessage, err)
}
})
t.Run("http 500 response error without reasons", func(t *testing.T) {
// Create the simulated HTTP response with a 500 status and an empty errors array
apiError := APIError{
Errors: []APIErrorReason{},
}
apiErrorBody, _ := json.Marshal(apiError)
bodyReader := io.NopCloser(bytes.NewBuffer(apiErrorBody))
resp := &http.Response{
StatusCode: http.StatusInternalServerError,
Body: bodyReader,
Header: http.Header{"Content-Type": []string{"application/json"}},
Request: &http.Request{Header: http.Header{"Accept": []string{"application/json"}}},
}
_, err := coupleAPIErrorsHTTP(resp, nil)
if err != nil {
t.Error("http error with no reasons should return no error")
}
})
t.Run("http response with nil error", func(t *testing.T) {
// Create the simulated HTTP response with a 500 status and a nil error
resp := &http.Response{
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(bytes.NewBuffer([]byte(`{"errors":[]}`))), // empty errors array in body
Header: http.Header{"Content-Type": []string{"application/json"}},
Request: &http.Request{Header: http.Header{"Accept": []string{"application/json"}}},
}
_, err := coupleAPIErrorsHTTP(resp, nil)
if err != nil {
t.Error("http error with no reasons should return no error")
}
})
t.Run("generic html error", func(t *testing.T) {
rawResponse := `<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx</center>
</body>
</html>`
route := "/v4/linode/instances/123"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(rawResponse))
}))
defer ts.Close()
client := &httpClient{
httpClient: ts.Client(),
}
expectedError := Error{
Code: http.StatusInternalServerError,
Message: "Unexpected Content-Type: Expected: application/json, Received: text/html\nResponse body: " + rawResponse,
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, ts.URL+route, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
req.Header.Set("Accept", "application/json")
resp, err := client.httpClient.Do(req)
if err != nil {
t.Fatalf("failed to send request: %v", err)
}
defer resp.Body.Close()
_, err = coupleAPIErrorsHTTP(resp, nil)
if diff := cmp.Diff(expectedError, err); diff != "" {
t.Errorf("expected error to match but got diff:\n%s", diff)
}
})
t.Run("bad gateway error", func(t *testing.T) {
rawResponse := `<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>`
buf := io.NopCloser(bytes.NewBuffer([]byte(rawResponse)))
resp := &http.Response{
StatusCode: http.StatusBadGateway,
Body: buf,
Header: http.Header{
"Content-Type": []string{"text/html"},
},
Request: &http.Request{
Header: http.Header{"Accept": []string{"application/json"}},
},
}
expectedError := Error{
Code: http.StatusBadGateway,
Message: http.StatusText(http.StatusBadGateway),
}
_, err := coupleAPIErrorsHTTP(resp, nil)
if !cmp.Equal(err, expectedError) {
t.Errorf("expected error %#v to match error %#v", err, expectedError)
}
})
}
func TestErrorIs(t *testing.T) {
t.Parallel()
defaultError := &Error{
Message: "default error",
Code: http.StatusInternalServerError,
}
for _, tc := range []struct {
testName string
err1 error
err2 error
expectedResult bool
}{
{
testName: "base errors.Is comparision",
err1: defaultError,
err2: defaultError,
expectedResult: true,
},
{
testName: "wrapped default",
err1: fmt.Errorf("test wrap: %w", defaultError),
err2: defaultError,
expectedResult: true,
},
{
testName: "deeply wrapped error",
err1: fmt.Errorf("wrap 1: %w", fmt.Errorf("wrap 2: %w", defaultError)),
err2: defaultError,
expectedResult: true,
},
{
testName: "default and Error from empty resty error",
err1: NewError(restyError("", "")),
err2: defaultError,
expectedResult: true,
},
{
testName: "default and Error from resty error with field",
err1: NewError(restyError("", "test field")),
err2: defaultError,
expectedResult: true,
},
{
testName: "default and Error from resty error with field and reason",
err1: NewError(restyError("test reason", "test field")),
err2: defaultError,
expectedResult: true,
},
{
testName: "default and Error from resty error with reason",
err1: NewError(restyError("test reason", "")),
err2: defaultError,
expectedResult: true,
},
{
testName: "error and nil",
err1: defaultError,
err2: nil,
expectedResult: false,
},
{
testName: "wrapped nil",
err1: fmt.Errorf("test wrap: %w", nil),
err2: defaultError,
expectedResult: false,
},
{
testName: "both errors are different nil", // NOTE: nils of different types are never equal
err1: nil,
err2: (*Error)(nil),
expectedResult: false,
},
{
testName: "different error types",
err1: errors.New("different error type"),
err2: defaultError,
expectedResult: false,
},
} {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
t.Parallel()
if errors.Is(tc.err1, tc.err2) != tc.expectedResult {
t.Errorf("expected %+#v to be equal %+#v", tc.err1, tc.err2)
}
})
}
}
func TestIsNotFound(t *testing.T) {
tests := []struct {
code int
match bool
}{
{code: http.StatusNotFound, match: true},
{code: http.StatusInternalServerError},
{code: http.StatusFound},
{code: http.StatusOK},
}
for _, tt := range tests {
name := http.StatusText(tt.code)
t.Run(name, func(t *testing.T) {
err := &Error{Code: tt.code}
if matches := IsNotFound(err); !matches && tt.match {
t.Errorf("should have matched %d", tt.code)
} else if matches && !tt.match {
t.Errorf("shoudl not have matched %d", tt.code)
}
})
}
}
func TestErrHasStatusCode(t *testing.T) {
tests := []struct {
name string
err error
codes []int
match bool
}{
{
name: "NotFound",
err: &Error{Code: http.StatusNotFound},
codes: []int{http.StatusNotFound},
match: true,
},
{
name: "NoCodes",
err: &Error{Code: http.StatusInternalServerError},
},
{
name: "MultipleCodes",
err: &Error{Code: http.StatusTeapot},
codes: []int{http.StatusBadRequest, http.StatusTeapot, http.StatusUnavailableForLegalReasons},
match: true,
},
{
name: "NotALinodeError",
err: io.EOF,
codes: []int{http.StatusTeapot},
},
{
name: "NoMatch",
err: &Error{Code: http.StatusTooEarly},
codes: []int{http.StatusLocked, http.StatusTooManyRequests},
},
{
name: "NilError",
codes: []int{http.StatusGone},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ErrHasStatus(tt.err, tt.codes...)
if !got && tt.match {
t.Errorf("should have matched")
} else if got && !tt.match {
t.Errorf("should not have matched")
}
})
}
}