-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaults_test.go
More file actions
557 lines (521 loc) · 18.1 KB
/
faults_test.go
File metadata and controls
557 lines (521 loc) · 18.1 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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package faults_test
import (
"errors"
"fmt"
"testing"
"time"
"github.com/deixis/faults"
)
// TestIs validates that each Is* function correctly identifies its error type,
// including through wrapping layers and negative cases.
func TestIs(t *testing.T) {
cause := fmt.Errorf("root cause")
tests := []struct {
scenario string
err error
is func(error) bool
want bool
}{
// Singletons
{
scenario: "NotFound singleton is recognized",
err: faults.NotFound,
is: faults.IsNotFound,
want: true,
},
{
scenario: "PermissionDenied singleton is recognized",
err: faults.PermissionDenied,
is: faults.IsPermissionDenied,
want: true,
},
{
scenario: "Unauthenticated singleton is recognized",
err: faults.Unauthenticated,
is: faults.IsUnauthenticated,
want: true,
},
{
scenario: "Unimplemented singleton is recognized",
err: faults.Unimplemented,
is: faults.IsUnimplemented,
want: true,
},
// Constructors
{
scenario: "Bad error without violations is recognized",
err: faults.Bad(),
is: faults.IsBad,
want: true,
},
{
scenario: "Bad error with field violations is recognized",
err: faults.Bad(&faults.FieldViolation{Field: "email", Description: "invalid format"}),
is: faults.IsBad,
want: true,
},
{
scenario: "FailedPrecondition error is recognized",
err: faults.FailedPrecondition(),
is: faults.IsFailedPrecondition,
want: true,
},
{
scenario: "Aborted error is recognized",
err: faults.Aborted(),
is: faults.IsAborted,
want: true,
},
{
scenario: "Unavailable error is recognized",
err: faults.Unavailable(0),
is: faults.IsUnavailable,
want: true,
},
{
scenario: "ResourceExhausted error is recognized",
err: faults.ResourceExhausted(),
is: faults.IsResourceExhausted,
want: true,
},
// With* wrappers
{
scenario: "WithNotFound wrapping a cause is recognized as NotFound",
err: faults.WithNotFound(cause),
is: faults.IsNotFound,
want: true,
},
{
scenario: "WithPermissionDenied wrapping a cause is recognized as PermissionDenied",
err: faults.WithPermissionDenied(cause),
is: faults.IsPermissionDenied,
want: true,
},
{
scenario: "WithUnauthenticated wrapping a cause is recognized as Unauthenticated",
err: faults.WithUnauthenticated(cause),
is: faults.IsUnauthenticated,
want: true,
},
{
scenario: "WithBad wrapping a cause is recognized as Bad",
err: faults.WithBad(cause),
is: faults.IsBad,
want: true,
},
{
scenario: "WithFailedPrecondition wrapping a cause is recognized as FailedPrecondition",
err: faults.WithFailedPrecondition(cause),
is: faults.IsFailedPrecondition,
want: true,
},
{
scenario: "WithAborted wrapping a cause is recognized as Aborted",
err: faults.WithAborted(cause),
is: faults.IsAborted,
want: true,
},
{
scenario: "WithUnavailable wrapping a cause is recognized as Unavailable",
err: faults.WithUnavailable(cause, 5*time.Second),
is: faults.IsUnavailable,
want: true,
},
{
scenario: "WithResourceExhausted wrapping a cause is recognized as ResourceExhausted",
err: faults.WithResourceExhausted(cause),
is: faults.IsResourceExhausted,
want: true,
},
{
scenario: "WithUnimplemented wrapping a cause is recognized as Unimplemented",
err: faults.WithUnimplemented(cause),
is: faults.IsUnimplemented,
want: true,
},
// Errors wrapped further with fmt.Errorf are still recognized
{
scenario: "ResourceExhausted wrapped in fmt.Errorf is still recognized",
err: fmt.Errorf("op failed: %w", faults.ResourceExhausted()),
is: faults.IsResourceExhausted,
want: true,
},
{
scenario: "NotFound wrapped in fmt.Errorf is still recognized",
err: fmt.Errorf("op failed: %w", faults.NotFound),
is: faults.IsNotFound,
want: true,
},
// Negative cases
{
scenario: "NotFound is not recognized as PermissionDenied",
err: faults.NotFound,
is: faults.IsPermissionDenied,
want: false,
},
{
scenario: "unrelated error is not recognized as NotFound",
err: fmt.Errorf("something went wrong"),
is: faults.IsNotFound,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.scenario, func(t *testing.T) {
got := tt.is(tt.err)
if got != tt.want {
t.Errorf("got %v, want %v (err: %v)", got, tt.want, tt.err)
}
})
}
}
// TestAs validates that each As* function correctly identifies and extracts
// its error type, including field values and through wrapping layers.
func TestAs(t *testing.T) {
cause := fmt.Errorf("root cause")
t.Run("NotFound singleton is extractable", func(t *testing.T) {
e, ok := faults.AsNotFound(faults.NotFound)
if !ok || e == nil {
t.Fatal("expected AsNotFound to extract a non-nil MissingFailure")
}
})
t.Run("PermissionDenied singleton is extractable", func(t *testing.T) {
e, ok := faults.AsPermissionDenied(faults.PermissionDenied)
if !ok || e == nil {
t.Fatal("expected AsPermissionDenied to extract a non-nil PermissionFailure")
}
})
t.Run("Unauthenticated singleton is extractable", func(t *testing.T) {
e, ok := faults.AsUnauthenticated(faults.Unauthenticated)
if !ok || e == nil {
t.Fatal("expected AsUnauthenticated to extract a non-nil AuthenticationFailure")
}
})
t.Run("Unimplemented singleton is extractable", func(t *testing.T) {
e, ok := faults.AsUnimplemented(faults.Unimplemented)
if !ok || e == nil {
t.Fatal("expected AsUnimplemented to extract a non-nil UnimplementedFailure")
}
})
t.Run("Bad error is extractable with field violations intact", func(t *testing.T) {
violation := &faults.FieldViolation{Field: "email", Description: "invalid format"}
e, ok := faults.AsBad(faults.Bad(violation))
if !ok {
t.Fatal("expected AsBad to return true")
}
if len(e.Violations) != 1 || e.Violations[0].Field != "email" {
t.Errorf("unexpected violations: %v", e.Violations)
}
})
t.Run("FailedPrecondition is extractable with precondition violations intact", func(t *testing.T) {
violation := &faults.PreconditionViolation{Type: "TOS", Subject: "google.com", Description: "not accepted"}
e, ok := faults.AsFailedPrecondition(faults.FailedPrecondition(violation))
if !ok {
t.Fatal("expected AsFailedPrecondition to return true")
}
if len(e.Violations) != 1 || e.Violations[0].Type != "TOS" {
t.Errorf("unexpected violations: %v", e.Violations)
}
})
t.Run("Aborted is extractable with conflict violations intact", func(t *testing.T) {
violation := &faults.ConflictViolation{Resource: "user:123", Description: "concurrent update"}
e, ok := faults.AsAborted(faults.Aborted(violation))
if !ok {
t.Fatal("expected AsAborted to return true")
}
if len(e.Violations) != 1 || e.Violations[0].Resource != "user:123" {
t.Errorf("unexpected violations: %v", e.Violations)
}
})
t.Run("Unavailable is extractable with retry delay intact", func(t *testing.T) {
e, ok := faults.AsUnavailable(faults.Unavailable(5 * time.Second))
if !ok {
t.Fatal("expected AsUnavailable to return true")
}
if e.RetryInfo.RetryDelay != 5*time.Second {
t.Errorf("expected retry delay 5s, got %v", e.RetryInfo.RetryDelay)
}
})
t.Run("ResourceExhausted is extractable with quota violations intact", func(t *testing.T) {
violation := &faults.QuotaViolation{Subject: "project:123", Description: "daily limit exceeded"}
e, ok := faults.AsResourceExhausted(faults.ResourceExhausted(violation))
if !ok {
t.Fatal("expected AsResourceExhausted to return true")
}
if len(e.Violations) != 1 || e.Violations[0].Subject != "project:123" {
t.Errorf("unexpected violations: %v", e.Violations)
}
})
t.Run("WithNotFound wrapping a cause is still extractable", func(t *testing.T) {
e, ok := faults.AsNotFound(faults.WithNotFound(cause))
if !ok || e == nil {
t.Fatal("expected AsNotFound to extract from a WithNotFound error")
}
})
t.Run("ResourceExhausted wrapped in fmt.Errorf is still extractable", func(t *testing.T) {
_, ok := faults.AsResourceExhausted(fmt.Errorf("op failed: %w", faults.ResourceExhausted()))
if !ok {
t.Fatal("expected AsResourceExhausted to extract through fmt.Errorf wrapping")
}
})
t.Run("NotFound is not extractable as PermissionDenied", func(t *testing.T) {
_, ok := faults.AsPermissionDenied(faults.NotFound)
if ok {
t.Fatal("expected AsPermissionDenied to return false for a NotFound error")
}
})
t.Run("unrelated error is not extractable as NotFound", func(t *testing.T) {
_, ok := faults.AsNotFound(fmt.Errorf("unrelated"))
if ok {
t.Fatal("expected AsNotFound to return false for an unrelated error")
}
})
}
// TestWith validates that With* functions wrap the parent error, preserving the
// fault type and keeping the original cause reachable in the error chain.
func TestWith(t *testing.T) {
cause := fmt.Errorf("root cause")
t.Run("WithNotFound preserves the parent cause in the error chain", func(t *testing.T) {
err := faults.WithNotFound(cause)
if !faults.IsNotFound(err) {
t.Error("expected IsNotFound to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
})
t.Run("WithPermissionDenied preserves the parent cause in the error chain", func(t *testing.T) {
err := faults.WithPermissionDenied(cause)
if !faults.IsPermissionDenied(err) {
t.Error("expected IsPermissionDenied to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
})
t.Run("WithUnauthenticated preserves the parent cause in the error chain", func(t *testing.T) {
err := faults.WithUnauthenticated(cause)
if !faults.IsUnauthenticated(err) {
t.Error("expected IsUnauthenticated to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
})
t.Run("WithBad preserves the parent cause and carries field violations", func(t *testing.T) {
violation := &faults.FieldViolation{Field: "name", Description: "required"}
err := faults.WithBad(cause, violation)
if !faults.IsBad(err) {
t.Error("expected IsBad to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
e, _ := faults.AsBad(err)
if len(e.Violations) != 1 || e.Violations[0].Field != "name" {
t.Errorf("unexpected violations: %v", e.Violations)
}
})
t.Run("WithFailedPrecondition preserves the parent cause in the error chain", func(t *testing.T) {
err := faults.WithFailedPrecondition(cause)
if !faults.IsFailedPrecondition(err) {
t.Error("expected IsFailedPrecondition to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
})
t.Run("WithAborted preserves the parent cause in the error chain", func(t *testing.T) {
err := faults.WithAborted(cause)
if !faults.IsAborted(err) {
t.Error("expected IsAborted to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
})
t.Run("WithUnavailable preserves the parent cause and carries the retry delay", func(t *testing.T) {
err := faults.WithUnavailable(cause, 3*time.Second)
if !faults.IsUnavailable(err) {
t.Error("expected IsUnavailable to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
e, _ := faults.AsUnavailable(err)
if e.RetryInfo.RetryDelay != 3*time.Second {
t.Errorf("expected retry delay 3s, got %v", e.RetryInfo.RetryDelay)
}
})
t.Run("WithResourceExhausted preserves the parent cause in the error chain", func(t *testing.T) {
err := faults.WithResourceExhausted(cause)
if !faults.IsResourceExhausted(err) {
t.Error("expected IsResourceExhausted to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
})
t.Run("WithUnimplemented preserves the parent cause in the error chain", func(t *testing.T) {
err := faults.WithUnimplemented(cause)
if !faults.IsUnimplemented(err) {
t.Error("expected IsUnimplemented to return true")
}
if !errors.Is(err, cause) {
t.Error("expected the original cause to be reachable via errors.Is")
}
})
}
// TestErrorMessage validates the human-readable message produced by each error type
// under various conditions (no violations, with violations, wrapping a cause).
func TestErrorMessage(t *testing.T) {
cause := fmt.Errorf("underlying issue")
tests := []struct {
scenario string
err error
wantMsg string
}{
// Singletons always return a static message regardless of context
{
scenario: "NotFound produces a not-found message",
err: faults.NotFound,
wantMsg: "resource not found",
},
{
scenario: "PermissionDenied produces a permission-denied message",
err: faults.PermissionDenied,
wantMsg: "permission denied",
},
{
scenario: "Unauthenticated produces an authentication-failure message",
err: faults.Unauthenticated,
wantMsg: "failed to authenticate request",
},
{
scenario: "Unimplemented produces an unimplemented message",
err: faults.Unimplemented,
wantMsg: "unimplemented (yet)",
},
// Constructors without violations produce a base message
{
scenario: "Bad without violations produces a bad-request message",
err: faults.Bad(),
wantMsg: "bad request",
},
{
scenario: "FailedPrecondition without violations produces a precondition-failure message",
err: faults.FailedPrecondition(),
wantMsg: "precondition failure",
},
{
scenario: "Aborted without violations produces a conflict message",
err: faults.Aborted(),
wantMsg: "conflict",
},
{
scenario: "Unavailable without retry delay produces a service-unavailable message",
err: faults.Unavailable(0),
wantMsg: "service temporarily unavailable",
},
{
scenario: "Unavailable with retry delay includes the delay in the message",
err: faults.Unavailable(5 * time.Second),
wantMsg: "service temporarily unavailable, retry in 5s",
},
{
scenario: "ResourceExhausted without violations produces a quota-failure message",
err: faults.ResourceExhausted(),
wantMsg: "quota failure",
},
// Constructors with violations include violation descriptions
{
scenario: "Bad with a field violation includes the violation description",
err: faults.Bad(&faults.FieldViolation{Field: "email", Description: "invalid format"}),
wantMsg: "invalid format",
},
{
scenario: "Bad with multiple field violations joins their descriptions",
err: faults.Bad(
&faults.FieldViolation{Field: "email", Description: "invalid format"},
&faults.FieldViolation{Field: "name", Description: "required"},
),
wantMsg: "invalid format. required",
},
{
scenario: "FailedPrecondition with a violation includes the violation description",
err: faults.FailedPrecondition(&faults.PreconditionViolation{Type: "TOS", Description: "not accepted"}),
wantMsg: "not accepted",
},
{
scenario: "Aborted with a conflict violation includes the violation description",
err: faults.Aborted(&faults.ConflictViolation{Resource: "user:1", Description: "concurrent update"}),
wantMsg: "concurrent update",
},
{
scenario: "ResourceExhausted with a quota violation includes the violation description",
err: faults.ResourceExhausted(&faults.QuotaViolation{Subject: "project:1", Description: "daily limit exceeded"}),
wantMsg: "daily limit exceeded",
},
// With* wrapping a cause includes the cause in the message
{
scenario: "WithBad wrapping a cause includes the cause in the message",
err: faults.WithBad(cause),
wantMsg: "bad request: underlying issue",
},
{
scenario: "WithFailedPrecondition wrapping a cause includes the cause in the message",
err: faults.WithFailedPrecondition(cause),
wantMsg: "precondition failure: underlying issue",
},
{
scenario: "WithAborted wrapping a cause includes the cause in the message",
err: faults.WithAborted(cause),
wantMsg: "conflict: underlying issue",
},
{
scenario: "WithResourceExhausted wrapping a cause includes the cause in the message",
err: faults.WithResourceExhausted(cause),
wantMsg: "quota failure: underlying issue",
},
}
for _, tt := range tests {
t.Run(tt.scenario, func(t *testing.T) {
got := tt.err.Error()
if got != tt.wantMsg {
t.Errorf("got %q, want %q", got, tt.wantMsg)
}
})
}
}
// TestViolationString validates the human-readable representation of each
// violation type with all fields populated.
func TestViolationString(t *testing.T) {
t.Run("FieldViolation formats field and description separated by a dash", func(t *testing.T) {
v := &faults.FieldViolation{Field: "email", Description: "invalid format"}
want := "email - invalid format"
if got := v.String(); got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("QuotaViolation formats subject and description separated by a dash", func(t *testing.T) {
v := &faults.QuotaViolation{Subject: "project:123", Description: "daily limit exceeded"}
want := "project:123 - daily limit exceeded"
if got := v.String(); got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("PreconditionViolation formats type, subject, and description separated by dashes", func(t *testing.T) {
v := &faults.PreconditionViolation{Type: "TOS", Subject: "google.com", Description: "not accepted"}
want := "TOS - google.com - not accepted"
if got := v.String(); got != want {
t.Errorf("got %q, want %q", got, want)
}
})
t.Run("ConflictViolation formats resource and description separated by a dash", func(t *testing.T) {
v := &faults.ConflictViolation{Resource: "user:123", Description: "concurrent update"}
want := "user:123 - concurrent update"
if got := v.String(); got != want {
t.Errorf("got %q, want %q", got, want)
}
})
}