forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
506 lines (419 loc) · 12.1 KB
/
error_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
package goa
import (
"encoding/json"
"errors"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Error", func() {
const (
id = "foo"
code = "invalid"
status = 400
detail = "error"
)
var meta = map[string]interface{}{"what": 42}
var gerr *ErrorResponse
BeforeEach(func() {
gerr = &ErrorResponse{ID: id, Code: code, Status: status, Detail: detail, Meta: meta}
})
It("serializes to JSON", func() {
b, err := json.Marshal(gerr)
Ω(err).ShouldNot(HaveOccurred())
Ω(string(b)).Should(Equal(`{"id":"foo","code":"invalid","status":400,"detail":"error","meta":{"what":42}}`))
})
})
var _ = Describe("InvalidParamTypeError", func() {
var valErr error
name := "param"
val := 42
expected := "43"
JustBeforeEach(func() {
valErr = InvalidParamTypeError(name, val, expected)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(name))
Ω(err.Detail).Should(ContainSubstring("%d", val))
Ω(err.Detail).Should(ContainSubstring(expected))
})
})
var _ = Describe("MissingParaerror", func() {
var valErr error
name := "param"
JustBeforeEach(func() {
valErr = MissingParamError(name)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(name))
})
})
var _ = Describe("InvalidAttributeTypeError", func() {
var valErr error
ctx := "ctx"
val := 42
expected := "43"
JustBeforeEach(func() {
valErr = InvalidAttributeTypeError(ctx, val, expected)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring("%d", val))
Ω(err.Detail).Should(ContainSubstring(expected))
})
})
var _ = Describe("MissingAttributeError", func() {
var valErr error
ctx := "ctx"
name := "param"
JustBeforeEach(func() {
valErr = MissingAttributeError(ctx, name)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring(name))
})
})
var _ = Describe("MissingHeaderError", func() {
var valErr error
name := "param"
JustBeforeEach(func() {
valErr = MissingHeaderError(name)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(name))
})
})
var _ = Describe("InvalidEnumValueError", func() {
var valErr error
ctx := "ctx"
val := 42
allowed := []interface{}{"43", "44"}
JustBeforeEach(func() {
valErr = InvalidEnumValueError(ctx, val, allowed)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring("%d", val))
Ω(err.Detail).Should(ContainSubstring(`"43", "44"`))
})
})
var _ = Describe("InvalidFormaerror", func() {
var valErr error
ctx := "ctx"
target := "target"
format := FormatDateTime
formatError := errors.New("boo")
JustBeforeEach(func() {
valErr = InvalidFormatError(ctx, target, format, formatError)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring(target))
Ω(err.Detail).Should(ContainSubstring("date-time"))
Ω(err.Detail).Should(ContainSubstring(formatError.Error()))
})
})
var _ = Describe("InvalidPatternError", func() {
var valErr error
ctx := "ctx"
target := "target"
pattern := "pattern"
JustBeforeEach(func() {
valErr = InvalidPatternError(ctx, target, pattern)
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring(target))
Ω(err.Detail).Should(ContainSubstring(pattern))
})
})
var _ = Describe("InvalidRangeError", func() {
var valErr error
var value interface{}
ctx := "ctx"
target := "target"
min := true
JustBeforeEach(func() {
valErr = InvalidRangeError(ctx, target, value, min)
})
Context("with an int value", func() {
BeforeEach(func() {
value = 42
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring("greater than or equal to"))
Ω(err.Detail).Should(ContainSubstring(fmt.Sprintf("%#v", value)))
Ω(err.Detail).Should(ContainSubstring(target))
})
})
Context("with a float64 value", func() {
BeforeEach(func() {
value = 42.42
})
It("creates a http error with no value truncation", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring("greater than or equal to"))
Ω(err.Detail).Should(ContainSubstring(fmt.Sprintf("%#v", value)))
Ω(err.Detail).Should(ContainSubstring(target))
})
})
})
var _ = Describe("InvalidLengthError", func() {
const ctx = "ctx"
const value = 42
const min = true
var target interface{}
var ln int
var valErr error
JustBeforeEach(func() {
valErr = InvalidLengthError(ctx, target, ln, value, min)
})
Context("on strings", func() {
BeforeEach(func() {
target = "target"
ln = len("target")
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring("greater than or equal to"))
Ω(err.Detail).Should(ContainSubstring(fmt.Sprintf("%#v", value)))
Ω(err.Detail).Should(ContainSubstring(target.(string)))
})
})
Context("on slices", func() {
BeforeEach(func() {
target = []string{"target1", "target2"}
ln = 2
})
It("creates a http error", func() {
Ω(valErr).ShouldNot(BeNil())
Ω(valErr).Should(BeAssignableToTypeOf(&ErrorResponse{}))
err := valErr.(*ErrorResponse)
Ω(err.Detail).Should(ContainSubstring(ctx))
Ω(err.Detail).Should(ContainSubstring("greater than or equal to"))
Ω(err.Detail).Should(ContainSubstring(fmt.Sprintf("%#v", value)))
Ω(err.Detail).Should(ContainSubstring(fmt.Sprintf("%#v", target)))
})
})
})
var _ = Describe("Merge", func() {
var err, err2 error
var mErr *ErrorResponse
BeforeEach(func() {
err = nil
err2 = nil
mErr = nil
})
JustBeforeEach(func() {
e := MergeErrors(err, err2)
if e != nil {
mErr = e.(*ErrorResponse)
}
})
Context("with two nil errors", func() {
It("returns a nil error", func() {
Ω(mErr).Should(BeNil())
})
})
Context("with a nil argument", func() {
const code = "foo"
BeforeEach(func() {
err = &ErrorResponse{Code: code}
})
It("returns the target", func() {
Ω(mErr).Should(Equal(err))
})
})
Context("with a nil target", func() {
Context("with the second argument a Error", func() {
const detail = "foo"
BeforeEach(func() {
err2 = &ErrorResponse{Detail: detail}
})
It("returns it", func() {
Ω(mErr).Should(Equal(err2))
})
})
Context("with the second argument not a Error", func() {
const detail = "foo"
BeforeEach(func() {
err2 = errors.New(detail)
})
It("wraps it into a Error", func() {
Ω(mErr).ShouldNot(BeNil())
Ω(mErr.Detail).Should(Equal(detail))
})
})
})
Context("with a non-nil target", func() {
const detail = "foo"
var status = 42
var code = "common"
BeforeEach(func() {
err = &ErrorResponse{Detail: detail, Status: status, Code: code}
})
Context("with another Error", func() {
const detail2 = "foo2"
var status2 = status
var code2 = code
var mErr2 *ErrorResponse
BeforeEach(func() {
mErr2 = &ErrorResponse{Detail: detail2, Status: status2, Code: code2}
err2 = mErr2
})
It("concatenates both error details", func() {
Ω(mErr.Detail).Should(Equal(detail + "; " + mErr2.Detail))
})
It("uses the common status", func() {
Ω(mErr.Status).Should(Equal(status))
})
It("uses the common code", func() {
Ω(mErr.Code).Should(Equal(code))
})
Context("with different code", func() {
BeforeEach(func() {
mErr2.Code = code + code
})
It("produces a bad_request error", func() {
Ω(mErr.Code).Should(Equal("bad_request"))
Ω(mErr.Status).Should(Equal(400))
Ω(mErr.Detail).Should(Equal(detail + "; " + mErr2.Detail))
})
})
Context("with different status", func() {
BeforeEach(func() {
mErr2.Status = status + status
})
It("produces a bad_request error", func() {
Ω(mErr.Code).Should(Equal("bad_request"))
Ω(mErr.Status).Should(Equal(400))
Ω(mErr.Detail).Should(Equal(detail + "; " + mErr2.Detail))
})
})
Context("with nil target metadata", func() {
BeforeEach(func() {
err.(*ErrorResponse).Meta = nil
})
Context("with nil/empty other metadata", func() {
BeforeEach(func() {
mErr2.Meta = nil
})
It("keeps nil target metadata if no other metadata", func() {
Ω(mErr.Meta).Should(BeNil())
})
})
Context("with other metadata", func() {
metaValues2 := map[string]interface{}{"foo": 1, "bar": 2}
BeforeEach(func() {
err.(*ErrorResponse).Meta = nil
mErr2.Meta = metaValues2
})
It("merges the metadata", func() {
Ω(mErr.Meta).Should(HaveLen(len(metaValues2)))
for k, v := range metaValues2 {
Ω(mErr.Meta[k]).Should(Equal(v))
}
})
})
})
Context("with target metadata", func() {
metaValues := map[string]interface{}{"baz": 3, "qux": 4}
BeforeEach(func() {
mv := make(map[string]interface{}, len(metaValues))
for k, v := range metaValues {
mv[k] = v
}
err.(*ErrorResponse).Meta = mv
})
Context("with nil/empty other metadata", func() {
BeforeEach(func() {
mErr2.Meta = nil
})
It("keeps target metadata if no other metadata", func() {
Ω(mErr.Meta).Should(HaveLen(len(metaValues)))
for k, v := range metaValues {
Ω(mErr.Meta[k]).Should(Equal(v))
}
})
})
Context("with other metadata", func() {
metaValues2 := map[string]interface{}{"foo": 1, "bar": 2}
BeforeEach(func() {
mErr2.Meta = metaValues2
})
It("merges the metadata", func() {
Ω(mErr.Meta).Should(HaveLen(len(metaValues) + len(metaValues2)))
for k, v := range metaValues {
Ω(mErr.Meta[k]).Should(Equal(v))
}
for k, v := range metaValues2 {
Ω(mErr.Meta[k]).Should(Equal(v))
}
})
})
})
Context("with metadata with a common key", func() {
const commonKey = "foo"
var metaValues = map[string]interface{}{commonKey: "bar", "foo2": 44}
var metaValues2 = map[string]interface{}{commonKey: 43, "baz": 42}
BeforeEach(func() {
mv := make(map[string]interface{}, len(metaValues))
for k, v := range metaValues {
mv[k] = v
}
err.(*ErrorResponse).Meta = mv
mErr2.Meta = metaValues2
})
It("merges the metadata", func() {
Ω(mErr.Meta).Should(HaveLen(len(metaValues) + len(metaValues2) - 1))
for k, v := range metaValues {
if k != commonKey {
Ω(mErr.Meta[k]).Should(Equal(v))
}
}
for k, v := range metaValues2 {
if k != commonKey {
Ω(mErr.Meta[k]).Should(Equal(v))
}
}
Ω(mErr.Meta[commonKey]).Should(Equal(metaValues2[commonKey]))
})
})
})
})
})