-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
557 lines (455 loc) · 13.1 KB
/
error_test.go
File metadata and controls
557 lines (455 loc) · 13.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 core_test
import (
. "dappco.re/go"
)
// --- Error Creation ---
func TestError_E_Good(t *T) {
err := E("user.Save", "failed to save", nil)
AssertError(t, err)
AssertContains(t, err.Error(), "user.Save")
AssertContains(t, err.Error(), "failed to save")
}
func TestError_E_WithCause_Good(t *T) {
cause := NewError("connection refused")
err := E("db.Connect", "database unavailable", cause)
AssertErrorIs(t, err, cause)
}
func TestError_Wrap_Good(t *T) {
cause := NewError("timeout")
err := Wrap(cause, "api.Call", "request failed")
AssertError(t, err)
AssertErrorIs(t, err, cause)
}
func TestError_Wrap_Nil_Good(t *T) {
err := Wrap(nil, "api.Call", "request failed")
AssertNil(t, err)
}
func TestError_WrapCode_Good(t *T) {
cause := NewError("invalid email")
err := WrapCode(cause, "VALIDATION_ERROR", "user.Validate", "bad input")
AssertError(t, err)
AssertEqual(t, "VALIDATION_ERROR", ErrorCode(err))
}
func TestError_NewCode_Good(t *T) {
err := NewCode("NOT_FOUND", "resource not found")
AssertError(t, err)
AssertEqual(t, "NOT_FOUND", ErrorCode(err))
}
// --- Error Introspection ---
func TestError_Operation_Good(t *T) {
err := E("brain.Recall", "search failed", nil)
AssertEqual(t, "brain.Recall", Operation(err))
}
func TestError_Operation_Bad(t *T) {
err := NewError("plain error")
AssertEqual(t, "", Operation(err))
}
func TestError_ErrorMessage_Good(t *T) {
err := E("op", "the message", nil)
AssertEqual(t, "the message", ErrorMessage(err))
}
func TestError_ErrorMessage_Plain(t *T) {
err := NewError("plain")
AssertEqual(t, "plain", ErrorMessage(err))
}
func TestError_ErrorMessage_Nil(t *T) {
AssertEqual(t, "", ErrorMessage(nil))
}
func TestError_Root_Good(t *T) {
root := NewError("root cause")
wrapped := Wrap(root, "layer1", "first wrap")
double := Wrap(wrapped, "layer2", "second wrap")
AssertEqual(t, root, Root(double))
}
func TestError_Root_Nil(t *T) {
AssertNil(t, Root(nil))
}
func TestError_StackTrace_Good(t *T) {
err := Wrap(E("inner", "cause", nil), "outer", "wrapper")
stack := StackTrace(err)
AssertLen(t, stack, 2)
AssertEqual(t, "outer", stack[0])
AssertEqual(t, "inner", stack[1])
}
func TestError_FormatStackTrace_Good(t *T) {
err := Wrap(E("a", "x", nil), "b", "y")
formatted := FormatStackTrace(err)
AssertEqual(t, "b -> a", formatted)
}
// --- ErrorLog ---
func TestError_ErrorLog_Good(t *T) {
c := New()
cause := NewError("boom")
r := c.Log().Error(cause, "test.Operation", "something broke")
AssertFalse(t, r.OK)
AssertErrorIs(t, r.Value.(error), cause)
}
func TestError_ErrorLog_Nil_Good(t *T) {
c := New()
r := c.Log().Error(nil, "test.Operation", "no error")
AssertTrue(t, r.OK)
}
func TestError_ErrorLog_Warn_Good(t *T) {
c := New()
cause := NewError("warning")
r := c.Log().Warn(cause, "test.Operation", "heads up")
AssertFalse(t, r.OK)
}
func TestError_ErrorLog_Must_Ugly(t *T) {
c := New()
AssertPanics(t, func() {
c.Log().Must(NewError("fatal"), "test.Operation", "must fail")
})
}
func TestError_ErrorLog_Must_Nil_Good(t *T) {
c := New()
AssertNotPanics(t, func() {
c.Log().Must(nil, "test.Operation", "no error")
})
}
// --- ErrorPanic ---
func TestError_ErrorPanic_Recover_Good(t *T) {
c := New()
// Should not panic — Recover catches it
AssertNotPanics(t, func() {
defer c.Error().Recover()
panic("test panic")
})
}
func TestError_ErrorPanic_SafeGo_Good(t *T) {
c := New()
done := make(chan bool, 1)
c.Error().SafeGo(func() {
done <- true
})
AssertTrue(t, <-done)
}
func TestError_ErrorPanic_SafeGo_Panic_Good(t *T) {
c := New()
done := make(chan bool, 1)
c.Error().SafeGo(func() {
defer func() { done <- true }()
panic("caught by SafeGo")
})
// SafeGo recovers — goroutine completes without crashing the process
<-done
}
// --- Standard Library Wrappers ---
func TestError_Is_Good(t *T) {
target := NewError("target")
wrapped := Wrap(target, "op", "msg")
AssertTrue(t, Is(wrapped, target))
}
func TestError_As_Good(t *T) {
err := E("op", "msg", nil)
var e *Err
AssertTrue(t, As(err, &e))
AssertEqual(t, "op", e.Operation)
}
func TestError_NewError_Good(t *T) {
err := NewError("simple error")
AssertEqual(t, "simple error", err.Error())
}
func TestError_ErrorJoin_Good(t *T) {
e1 := NewError("first")
e2 := NewError("second")
joined := ErrorJoin(e1, e2)
AssertErrorIs(t, joined, e1)
AssertErrorIs(t, joined, e2)
}
// --- ErrorPanic Crash Reports ---
func TestError_ErrorPanic_Reports_Good(t *T) {
dir := t.TempDir()
path := Path(dir, "crashes.json")
// Create ErrorPanic with file output
c := New()
// Access internals via a crash that writes to file
// Since ErrorPanic fields are unexported, we test via Recover
_ = c
_ = path
// Crash reporting needs ErrorPanic configured with filePath — tested indirectly
}
// --- ErrorPanic Crash File ---
func TestError_ErrorPanic_CrashFile_Good(t *T) {
dir := t.TempDir()
path := Path(dir, "crashes.json")
// Create Core, trigger a panic through SafeGo, check crash file
// ErrorPanic.filePath is unexported — but we can test via the package-level
// error handling that writes crash reports
// For now, test that Reports handles missing file gracefully
c := New()
r := c.Error().Reports(5)
AssertFalse(t, r.OK)
AssertNil(t, r.Value)
_ = path
}
// --- Error formatting branches ---
func TestError_Err_Error_WithCode_Good(t *T) {
err := WrapCode(NewError("bad"), "INVALID", "validate", "input failed")
AssertContains(t, err.Error(), "[INVALID]")
AssertContains(t, err.Error(), "validate")
AssertContains(t, err.Error(), "bad")
}
func TestError_Err_Error_CodeNoCause_Good(t *T) {
err := NewCode("NOT_FOUND", "resource missing")
AssertContains(t, err.Error(), "[NOT_FOUND]")
AssertContains(t, err.Error(), "resource missing")
}
func TestError_Err_Error_NoOp_Good(t *T) {
err := &Err{Message: "bare error"}
AssertEqual(t, "bare error", err.Error())
}
func TestError_WrapCode_NilErr_EmptyCode_Good(t *T) {
err := WrapCode(nil, "", "op", "msg")
AssertNil(t, err)
}
func TestError_Wrap_PreservesCode_Good(t *T) {
inner := WrapCode(NewError("root"), "AUTH_FAIL", "auth", "denied")
outer := Wrap(inner, "handler", "request failed")
AssertEqual(t, "AUTH_FAIL", ErrorCode(outer))
}
func TestError_ErrorLog_Warn_Nil_Good(t *T) {
c := New()
r := c.LogWarn(nil, "op", "msg")
AssertTrue(t, r.OK)
}
func TestError_ErrorLog_Error_Nil_Good(t *T) {
c := New()
r := c.LogError(nil, "op", "msg")
AssertTrue(t, r.OK)
}
func TestError_AllOperations_Good(t *T) {
err := Wrap(E("agent.Token", "expired", nil), "agent.Dispatch", "failed")
var ops []string
for op := range AllOperations(err) {
ops = append(ops, op)
}
AssertEqual(t, []string{"agent.Dispatch", "agent.Token"}, ops)
}
func TestError_AllOperations_Bad(t *T) {
var ops []string
for op := range AllOperations(NewError("plain failure")) {
ops = append(ops, op)
}
AssertEmpty(t, ops)
}
func TestError_AllOperations_Ugly(t *T) {
var ops []string
for op := range AllOperations(nil) {
ops = append(ops, op)
}
AssertEmpty(t, ops)
}
func TestError_AllOperationsBreak_Bad(t *T) {
err := Wrap(E("agent.Token", "expired", nil), "agent.Dispatch", "failed")
var ops []string
for op := range AllOperations(err) {
ops = append(ops, op)
break
}
AssertEqual(t, []string{"agent.Dispatch"}, ops)
}
type plainErr struct{ msg string }
func (e *plainErr) Error() string { return e.msg }
func TestError_As_Bad(t *T) {
// A non-*Err error never matches the *Err target.
var structured *Err
AssertFalse(t, As(&plainErr{msg: "plain failure"}, &structured))
AssertNil(t, structured)
}
func TestError_As_Ugly(t *T) {
var structured *Err
AssertFalse(t, As(nil, &structured))
AssertNil(t, structured)
}
func TestError_E_Bad(t *T) {
err := E("", "", nil)
AssertError(t, err)
AssertEqual(t, "", err.Error())
}
func TestError_E_Ugly(t *T) {
err := E("agent.Dispatch", "", AnError)
AssertContains(t, err.Error(), "agent.Dispatch")
AssertErrorIs(t, err, AnError)
}
func TestError_Err_Error_Good(t *T) {
err := &Err{Operation: "agent.Dispatch", Message: "failed", Cause: AnError, Code: "agent.failed"}
AssertContains(t, err.Error(), "agent.Dispatch")
AssertContains(t, err.Error(), "[agent.failed]")
AssertContains(t, err.Error(), AnError.Error())
}
func TestError_Err_Error_Bad(t *T) {
err := &Err{}
AssertEqual(t, "", err.Error())
}
func TestError_Err_Error_Ugly(t *T) {
err := &Err{Message: "session refused", Code: "session.refused"}
AssertEqual(t, "session refused [session.refused]", err.Error())
}
func TestError_Err_Unwrap_Good(t *T) {
err := &Err{Cause: AnError}
AssertEqual(t, AnError, err.Unwrap())
}
func TestError_Err_Unwrap_Bad(t *T) {
err := &Err{}
AssertNil(t, err.Unwrap())
}
func TestError_Err_Unwrap_Ugly(t *T) {
root := NewCode("agent.refused", "dispatch refused")
err := &Err{Cause: Wrap(root, "agent.Dispatch", "failed")}
AssertErrorIs(t, err.Unwrap(), root)
}
func TestError_ErrorCode_Good(t *T) {
err := NewCode("agent.refused", "dispatch refused")
AssertEqual(t, "agent.refused", ErrorCode(err))
}
func TestError_ErrorCode_Bad(t *T) {
AssertEqual(t, "", ErrorCode(NewError("plain failure")))
}
func TestError_ErrorCode_Ugly(t *T) {
AssertEqual(t, "", ErrorCode(nil))
}
func TestError_ErrorJoin_Bad(t *T) {
AssertNil(t, ErrorJoin(nil, nil))
}
func TestError_ErrorJoin_Ugly(t *T) {
joined := ErrorJoin(nil, AnError)
AssertErrorIs(t, joined, AnError)
}
func TestError_ErrorLog_Error_Good(t *T) {
r := New().Log().Error(AnError, "agent.Dispatch", "failed")
AssertFalse(t, r.OK)
AssertErrorIs(t, r.Value.(error), AnError)
}
func TestError_ErrorLog_Error_Bad(t *T) {
r := New().Log().Error(nil, "agent.Dispatch", "no failure")
AssertTrue(t, r.OK)
}
func TestError_ErrorLog_Error_Ugly(t *T) {
r := (&ErrorLog{}).Error(AnError, "agent.Dispatch", "failed")
AssertFalse(t, r.OK)
AssertErrorIs(t, r.Value.(error), AnError)
}
func TestError_ErrorLog_Must_Good(t *T) {
AssertNotPanics(t, func() {
New().Log().Must(nil, "agent.Dispatch", "ready")
})
}
func TestError_ErrorLog_Must_Bad(t *T) {
AssertPanicsWithError(t, "dispatch failed", func() {
New().Log().Must(AnError, "agent.Dispatch", "dispatch failed")
})
}
func TestError_ErrorLog_Warn_Bad(t *T) {
r := New().Log().Warn(nil, "agent.Dispatch", "no warning")
AssertTrue(t, r.OK)
}
func TestError_ErrorLog_Warn_Ugly(t *T) {
r := (&ErrorLog{}).Warn(AnError, "agent.Dispatch", "degraded")
AssertFalse(t, r.OK)
AssertErrorIs(t, r.Value.(error), AnError)
}
func TestError_ErrorMessage_Bad(t *T) {
AssertEqual(t, "plain failure", ErrorMessage(&plainErr{msg: "plain failure"}))
}
func TestError_ErrorMessage_Ugly(t *T) {
AssertEqual(t, "", ErrorMessage(nil))
}
func TestError_ErrorPanic_Recover_Bad(t *T) {
AssertNotPanics(t, func() {
New().Error().Recover()
})
}
func TestError_ErrorPanic_Recover_Ugly(t *T) {
var h *ErrorPanic
AssertNotPanics(t, func() {
h.Recover()
})
}
func TestError_ErrorPanic_Reports_Bad(t *T) {
r := New().Error().Reports(1)
AssertFalse(t, r.OK)
AssertNil(t, r.Value)
}
func TestError_ErrorPanic_Reports_Ugly(t *T) {
r := New().Error().Reports(0)
AssertFalse(t, r.OK)
AssertNil(t, r.Value)
}
func TestError_ErrorPanic_SafeGo_Bad(t *T) {
done := make(chan bool, 1)
New().Error().SafeGo(func() {
defer func() { done <- true }()
panic("agent worker failed")
})
AssertTrue(t, <-done)
}
func TestError_ErrorPanic_SafeGo_Ugly(t *T) {
done := make(chan bool, 1)
New().Error().SafeGo(func() {
done <- true
})
AssertTrue(t, <-done)
}
func TestError_FormatStackTrace_Bad(t *T) {
AssertEqual(t, "", FormatStackTrace(NewError("plain failure")))
}
func TestError_FormatStackTrace_Ugly(t *T) {
AssertEqual(t, "", FormatStackTrace(nil))
}
func TestError_Is_Bad(t *T) {
AssertFalse(t, Is(NewError("left"), NewError("right")))
}
func TestError_Is_Ugly(t *T) {
AssertTrue(t, Is(nil, nil))
}
func TestError_NewCode_Bad(t *T) {
err := NewCode("", "dispatch refused")
AssertEqual(t, "dispatch refused", err.Error())
AssertEqual(t, "", ErrorCode(err))
}
func TestError_NewCode_Ugly(t *T) {
err := NewCode("", "")
AssertEqual(t, "", err.Error())
}
func TestError_NewError_Bad(t *T) {
err := NewError("")
AssertEqual(t, "", err.Error())
}
func TestError_NewError_Ugly(t *T) {
err := NewError("session\nrefused")
AssertContains(t, err.Error(), "session\nrefused")
}
func TestError_Operation_Ugly(t *T) {
AssertEqual(t, "", Operation(nil))
}
func TestError_Root_Bad(t *T) {
err := NewError("plain failure")
AssertEqual(t, err, Root(err))
}
func TestError_Root_Ugly(t *T) {
AssertNil(t, Root(nil))
}
func TestError_StackTrace_Bad(t *T) {
AssertEmpty(t, StackTrace(NewError("plain failure")))
}
func TestError_StackTrace_Ugly(t *T) {
AssertEmpty(t, StackTrace(nil))
}
func TestError_Wrap_Bad(t *T) {
AssertNil(t, Wrap(nil, "agent.Dispatch", "failed"))
}
func TestError_Wrap_Ugly(t *T) {
inner := NewCode("agent.refused", "dispatch refused")
err := Wrap(inner, "agent.Dispatch", "failed")
AssertEqual(t, "agent.refused", ErrorCode(err))
}
func TestError_WrapCode_Bad(t *T) {
AssertNil(t, WrapCode(nil, "", "agent.Dispatch", "failed"))
}
func TestError_WrapCode_Ugly(t *T) {
err := WrapCode(nil, "agent.refused", "agent.Dispatch", "failed")
AssertError(t, err)
AssertEqual(t, "agent.refused", ErrorCode(err))
AssertNil(t, Root(err).(*Err).Cause)
}