-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
457 lines (417 loc) · 14.2 KB
/
Copy patherrors_test.go
File metadata and controls
457 lines (417 loc) · 14.2 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
package errors_test
import (
stderrors "errors"
"fmt"
"strings"
"testing"
errs "github.com/pleme-io/errors-go"
)
// --- Severity.String ---------------------------------------------------------
func TestSeverity_String(t *testing.T) {
tests := []struct {
name string
sev errs.Severity
want string
}{
{"notice", errs.SeverityNotice, "notice"},
{"warning", errs.SeverityWarning, "warning"},
{"error", errs.SeverityError, "error"},
{"unknown", errs.Severity(42), "severity(42)"},
{"negative", errs.Severity(-1), "severity(-1)"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.sev.String(); got != tt.want {
t.Errorf("Severity(%d).String() = %q, want %q", int(tt.sev), got, tt.want)
}
})
}
}
// The ladder must be ordered so that comparisons classify "more severe".
func TestSeverity_Ordering(t *testing.T) {
if !(errs.SeverityNotice < errs.SeverityWarning && errs.SeverityWarning < errs.SeverityError) {
t.Fatalf("severity ladder not ordered: notice=%d warning=%d error=%d",
errs.SeverityNotice, errs.SeverityWarning, errs.SeverityError)
}
if errs.DefaultSeverity != errs.SeverityError {
t.Errorf("DefaultSeverity = %v, want error", errs.DefaultSeverity)
}
}
// --- New ---------------------------------------------------------------------
func TestNew(t *testing.T) {
tests := []struct {
name string
msg string
opts []errs.Option
wantMsg string
wantSev errs.Severity
wantCode string
}{
{
name: "defaults to error severity, no code",
msg: "boom",
wantMsg: "boom", wantSev: errs.SeverityError, wantCode: "",
},
{
name: "with severity",
msg: "soft",
opts: []errs.Option{errs.WithSeverity(errs.SeverityWarning)},
wantMsg: "soft", wantSev: errs.SeverityWarning, wantCode: "",
},
{
name: "with code",
msg: "missing",
opts: []errs.Option{errs.WithCode("E_NOT_FOUND")},
wantMsg: "missing", wantSev: errs.SeverityError, wantCode: "E_NOT_FOUND",
},
{
name: "with both, severity then code",
msg: "note",
opts: []errs.Option{errs.WithSeverity(errs.SeverityNotice), errs.WithCode("E_NOTE")},
wantMsg: "note", wantSev: errs.SeverityNotice, wantCode: "E_NOTE",
},
{
name: "later option of same kind wins",
msg: "x",
opts: []errs.Option{errs.WithSeverity(errs.SeverityNotice), errs.WithSeverity(errs.SeverityError)},
wantMsg: "x", wantSev: errs.SeverityError, wantCode: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := errs.New(tt.msg, tt.opts...)
if err.Error() != tt.wantMsg {
t.Errorf("Error() = %q, want %q", err.Error(), tt.wantMsg)
}
if got := errs.SeverityOf(err); got != tt.wantSev {
t.Errorf("SeverityOf = %v, want %v", got, tt.wantSev)
}
if got := errs.CodeOf(err); got != tt.wantCode {
t.Errorf("CodeOf = %q, want %q", got, tt.wantCode)
}
})
}
}
// --- Wrap --------------------------------------------------------------------
// Wrap(nil) must be nil so callers can wrap unconditionally in a tail position.
func TestWrap_NilIsNil(t *testing.T) {
if got := errs.Wrap(nil, "context"); got != nil {
t.Fatalf("Wrap(nil, ...) = %v, want nil", got)
}
// Even with options, nil in => nil out.
if got := errs.Wrap(nil, "context", errs.WithSeverity(errs.SeverityNotice), errs.WithCode("X")); got != nil {
t.Fatalf("Wrap(nil, ..., opts) = %v, want nil", got)
}
}
func TestWrap_Message(t *testing.T) {
base := errs.New("root cause")
wrapped := errs.Wrap(base, "while doing thing")
want := "while doing thing: root cause"
if got := wrapped.Error(); got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
}
// An empty wrap message renders the cause alone (no dangling ": ").
func TestWrap_EmptyMessageRendersCause(t *testing.T) {
base := errs.New("root cause")
wrapped := errs.Wrap(base, "")
if got := wrapped.Error(); got != "root cause" {
t.Errorf("Error() = %q, want %q", got, "root cause")
}
}
func TestWrap_MultiLayerMessage(t *testing.T) {
base := stderrors.New("disk full")
l1 := errs.Wrap(base, "write file")
l2 := errs.Wrap(l1, "persist state")
want := "persist state: write file: disk full"
if got := l2.Error(); got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
}
// --- Unwrap / Is / As across wraps ------------------------------------------
func TestIs_AcrossWraps(t *testing.T) {
sentinel := errs.New("not found", errs.WithCode("E_NOT_FOUND"))
l1 := errs.Wrap(sentinel, "load tenant")
l2 := errs.Wrap(l1, "reconcile")
l3 := errs.Wrap(l2, "serve request")
if !stderrors.Is(l3, sentinel) {
t.Errorf("Is(l3, sentinel) = false, want true (sentinel must match across 3 wraps)")
}
other := errs.New("other")
if stderrors.Is(l3, other) {
t.Errorf("Is(l3, other) = true, want false")
}
}
// Is must also reach a plain stdlib sentinel wrapped by our Wrap.
func TestIs_StdlibSentinelAcrossWraps(t *testing.T) {
var ErrStd = stderrors.New("std sentinel")
wrapped := errs.Wrap(errs.Wrap(ErrStd, "inner"), "outer")
if !stderrors.Is(wrapped, ErrStd) {
t.Errorf("Is(wrapped, ErrStd) = false, want true")
}
}
// A typed *Error can be recovered with errors.As across wraps.
func TestAs_AcrossWraps(t *testing.T) {
leaf := errs.New("typed leaf", errs.WithCode("E_LEAF"), errs.WithSeverity(errs.SeverityWarning))
wrapped := errs.Wrap(errs.Wrap(leaf, "mid"), "top")
var target *errs.Error
if !stderrors.As(wrapped, &target) {
t.Fatalf("As(wrapped, *Error) = false, want true")
}
// As finds the OUTERMOST *Error, which is the top wrapper.
if target.Error() != "top: mid: typed leaf" {
t.Errorf("As matched %q, want outermost wrapper", target.Error())
}
}
// Unwrap returns the immediate cause, nil for a leaf.
func TestUnwrap(t *testing.T) {
leaf := errs.New("leaf")
if got := stderrors.Unwrap(leaf); got != nil {
t.Errorf("Unwrap(leaf) = %v, want nil", got)
}
wrapped := errs.Wrap(leaf, "ctx")
if got := stderrors.Unwrap(wrapped); got != leaf {
t.Errorf("Unwrap(wrapped) = %v, want leaf", got)
}
}
// Interop with fmt.Errorf %w in the middle of the chain.
func TestInterop_FmtErrorf(t *testing.T) {
leaf := errs.New("leaf", errs.WithCode("E_LEAF"))
mid := fmt.Errorf("fmt layer: %w", leaf)
top := errs.Wrap(mid, "top")
if !stderrors.Is(top, leaf) {
t.Errorf("Is(top, leaf) = false, want true through fmt.Errorf")
}
if got := errs.CodeOf(top); got != "E_LEAF" {
t.Errorf("CodeOf(top) = %q, want E_LEAF through fmt.Errorf", got)
}
}
// --- Severity propagation ----------------------------------------------------
func TestSeverityOf(t *testing.T) {
tests := []struct {
name string
err error
want errs.Severity
}{
{"nil defaults to error", nil, errs.SeverityError},
{"plain stdlib defaults to error", stderrors.New("x"), errs.SeverityError},
{"leaf notice", errs.New("n", errs.WithSeverity(errs.SeverityNotice)), errs.SeverityNotice},
{"leaf warning", errs.New("w", errs.WithSeverity(errs.SeverityWarning)), errs.SeverityWarning},
{
name: "wrap inherits inner severity by default",
err: errs.Wrap(errs.New("n", errs.WithSeverity(errs.SeverityNotice)), "ctx"),
want: errs.SeverityNotice,
},
{
name: "wrap inherits through multiple layers",
err: errs.Wrap(
errs.Wrap(errs.New("w", errs.WithSeverity(errs.SeverityWarning)), "mid"),
"top"),
want: errs.SeverityWarning,
},
{
name: "explicit wrap severity overrides inner",
err: errs.Wrap(
errs.New("n", errs.WithSeverity(errs.SeverityNotice)),
"ctx", errs.WithSeverity(errs.SeverityError)),
want: errs.SeverityError,
},
{
name: "wrap of plain stdlib defaults to error",
err: errs.Wrap(stderrors.New("x"), "ctx"),
want: errs.SeverityError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := errs.SeverityOf(tt.err); got != tt.want {
t.Errorf("SeverityOf = %v, want %v", got, tt.want)
}
})
}
}
// The Severity()/Code() methods on a directly held *Error report that layer.
func TestErrorMethods_SingleLayer(t *testing.T) {
var target *errs.Error
if !stderrors.As(errs.New("x", errs.WithSeverity(errs.SeverityWarning), errs.WithCode("C")), &target) {
t.Fatal("As failed")
}
if target.Severity() != errs.SeverityWarning {
t.Errorf("Severity() = %v, want warning", target.Severity())
}
if target.Code() != "C" {
t.Errorf("Code() = %q, want C", target.Code())
}
}
// --- Code extraction ---------------------------------------------------------
func TestCodeOf(t *testing.T) {
tests := []struct {
name string
err error
want string
}{
{"nil", nil, ""},
{"plain stdlib", stderrors.New("x"), ""},
{"leaf with code", errs.New("x", errs.WithCode("E_X")), "E_X"},
{"leaf without code", errs.New("x"), ""},
{
name: "wrap exposes inner code (context-only wrap)",
err: errs.Wrap(errs.New("x", errs.WithCode("E_INNER")), "ctx"),
want: "E_INNER",
},
{
name: "outer code overrides inner",
err: errs.Wrap(
errs.New("x", errs.WithCode("E_INNER")),
"ctx", errs.WithCode("E_OUTER")),
want: "E_OUTER",
},
{
name: "first non-empty code wins, skipping empty outer layers",
err: errs.Wrap(
errs.Wrap(errs.New("x", errs.WithCode("E_DEEP")), "mid"),
"top"),
want: "E_DEEP",
},
{
name: "no code anywhere in chain",
err: errs.Wrap(errs.Wrap(errs.New("x"), "mid"), "top"),
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := errs.CodeOf(tt.err); got != tt.want {
t.Errorf("CodeOf = %q, want %q", got, tt.want)
}
})
}
}
// --- Join --------------------------------------------------------------------
func TestJoin_NilHandling(t *testing.T) {
if got := errs.Join(); got != nil {
t.Errorf("Join() = %v, want nil", got)
}
if got := errs.Join(nil, nil); got != nil {
t.Errorf("Join(nil, nil) = %v, want nil", got)
}
}
func TestJoin_IsReachesEveryMember(t *testing.T) {
a := errs.New("a", errs.WithCode("E_A"))
b := stderrors.New("b")
c := errs.New("c", errs.WithSeverity(errs.SeverityWarning))
joined := errs.Join(a, nil, b, c) // nil dropped
for _, member := range []error{a, b, c} {
if !stderrors.Is(joined, member) {
t.Errorf("Is(joined, %v) = false, want true", member)
}
}
}
func TestJoin_AsReachesMember(t *testing.T) {
a := stderrors.New("plain")
b := errs.New("typed", errs.WithCode("E_B"))
joined := errs.Join(a, b)
var target *errs.Error
if !stderrors.As(joined, &target) {
t.Fatalf("As(joined, *Error) = false, want true")
}
if target.Code() != "E_B" {
t.Errorf("As matched code %q, want E_B", target.Code())
}
}
func TestJoin_Message(t *testing.T) {
joined := errs.Join(stderrors.New("first"), stderrors.New("second"))
got := joined.Error()
if !strings.Contains(got, "first") || !strings.Contains(got, "second") {
t.Errorf("Join Error() = %q, want both members", got)
}
if want := "first\nsecond"; got != want {
t.Errorf("Join Error() = %q, want %q (newline separated)", got, want)
}
}
// Join's severity is the most severe member; its code is the first non-empty.
func TestJoin_SeverityAndCode(t *testing.T) {
tests := []struct {
name string
errs []error
wantSev errs.Severity
wantCode string
}{
{
name: "most severe member wins (notice + error => error)",
errs: []error{errs.New("n", errs.WithSeverity(errs.SeverityNotice)), errs.New("e", errs.WithSeverity(errs.SeverityError))},
wantSev: errs.SeverityError, wantCode: "",
},
{
name: "all notice stays notice",
errs: []error{errs.New("n1", errs.WithSeverity(errs.SeverityNotice)), errs.New("n2", errs.WithSeverity(errs.SeverityNotice))},
wantSev: errs.SeverityNotice, wantCode: "",
},
{
name: "warning + notice => warning",
errs: []error{errs.New("n", errs.WithSeverity(errs.SeverityNotice)), errs.New("w", errs.WithSeverity(errs.SeverityWarning))},
wantSev: errs.SeverityWarning, wantCode: "",
},
{
name: "plain stdlib member contributes default error severity",
errs: []error{errs.New("n", errs.WithSeverity(errs.SeverityNotice)), stderrors.New("plain")},
wantSev: errs.SeverityError, wantCode: "",
},
{
name: "first non-empty code wins in argument order",
errs: []error{errs.New("a"), errs.New("b", errs.WithCode("E_B")), errs.New("c", errs.WithCode("E_C"))},
wantSev: errs.SeverityError, wantCode: "E_B",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
joined := errs.Join(tt.errs...)
if got := errs.SeverityOf(joined); got != tt.wantSev {
t.Errorf("SeverityOf(joined) = %v, want %v", got, tt.wantSev)
}
if got := errs.CodeOf(joined); got != tt.wantCode {
t.Errorf("CodeOf(joined) = %q, want %q", got, tt.wantCode)
}
})
}
}
// A Join aggregate can itself be wrapped, and metadata still propagates out.
func TestJoin_Wrapped(t *testing.T) {
joined := errs.Join(
errs.New("a", errs.WithSeverity(errs.SeverityNotice)),
errs.New("b", errs.WithSeverity(errs.SeverityWarning), errs.WithCode("E_B")),
)
wrapped := errs.Wrap(joined, "aggregate failed")
if got := errs.SeverityOf(wrapped); got != errs.SeverityWarning {
t.Errorf("SeverityOf(wrapped join) = %v, want warning (inherited)", got)
}
if got := errs.CodeOf(wrapped); got != "E_B" {
t.Errorf("CodeOf(wrapped join) = %q, want E_B", got)
}
if !strings.HasPrefix(wrapped.Error(), "aggregate failed: ") {
t.Errorf("Error() = %q, want prefix %q", wrapped.Error(), "aggregate failed: ")
}
}
// --- Sentinel usage pattern (thiserror analog) -------------------------------
func TestSentinel_Pattern(t *testing.T) {
// A package would define these at top level.
var (
ErrNotFound = errs.New("resource not found", errs.WithCode("E_NOT_FOUND"))
ErrPermission = errs.New("permission denied", errs.WithCode("E_PERM"), errs.WithSeverity(errs.SeverityWarning))
)
// Returned deep in a call stack, wrapped with context on the way out.
deep := func() error { return errs.Wrap(ErrNotFound, "fetch from store") }
err := errs.Wrap(deep(), "handle request")
if !stderrors.Is(err, ErrNotFound) {
t.Errorf("Is(err, ErrNotFound) = false, want true")
}
if stderrors.Is(err, ErrPermission) {
t.Errorf("Is(err, ErrPermission) = true, want false")
}
if got := errs.CodeOf(err); got != "E_NOT_FOUND" {
t.Errorf("CodeOf = %q, want E_NOT_FOUND", got)
}
if got := errs.SeverityOf(err); got != errs.SeverityError {
t.Errorf("SeverityOf = %v, want error (ErrNotFound default)", got)
}
}