-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherr_test.go
More file actions
1656 lines (1531 loc) · 45.9 KB
/
err_test.go
File metadata and controls
1656 lines (1531 loc) · 45.9 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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package errors
import (
"strings"
"testing"
)
func TestNew(t *testing.T) {
err := New("something went wrong")
if err == nil {
t.Fatal("expected non-nil error")
}
if err.Message() != "something went wrong" {
t.Errorf("expected message 'something went wrong', got '%s'", err.Message())
}
if err.File() == "" {
t.Error("expected non-empty file")
}
if err.Line() == 0 {
t.Error("expected non-zero line")
}
}
func TestNewEmpty(t *testing.T) {
err := New()
if err.Message() != "<empty>" {
t.Errorf("expected '<empty>', got '%s'", err.Message())
}
}
func TestNewf(t *testing.T) {
err := Newf("error %s", "42")
if err.Message() != "error 42" {
t.Errorf("expected 'error 42', got '%s'", err.Message())
}
}
func TestNewWithCode(t *testing.T) {
err := NewWithCode("ERR001", "bad request")
if err.Code() != "ERR001" {
t.Errorf("expected code 'ERR001', got '%s'", err.Code())
}
if !err.HasCode() {
t.Error("expected HasCode to be true")
}
}
func TestNewWithMetadata(t *testing.T) {
meta := map[string]any{"key": "value"}
err := NewWithMetadata(meta, "with meta")
if !err.HasMetadata() {
t.Error("expected HasMetadata to be true")
}
if err.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", err.Metadata()["key"])
}
}
func TestNewWithCodeAndMetadata(t *testing.T) {
meta := map[string]any{"x": 1}
err := NewWithCodeAndMetadata("CODE", meta, "msg")
if err.Code() != "CODE" {
t.Errorf("expected code 'CODE', got '%s'", err.Code())
}
if err.Metadata()["x"] != 1 {
t.Errorf("expected metadata x=1")
}
}
func TestNewWithCodef(t *testing.T) {
err := NewWithCodef("ERR", "value is %s", "bad")
if err.Code() != "ERR" {
t.Errorf("expected code 'ERR', got '%s'", err.Code())
}
if err.Message() != "value is bad" {
t.Errorf("expected 'value is bad', got '%s'", err.Message())
}
}
func TestNewWithMetadataf(t *testing.T) {
meta := map[string]any{"k": "v"}
err := NewWithMetadataf(meta, "msg %s", "here")
if err.Message() != "msg here" {
t.Errorf("expected 'msg here', got '%s'", err.Message())
}
}
func TestNewWithCodeAndMetadataf(t *testing.T) {
meta := map[string]any{"k": "v"}
err := NewWithCodeAndMetadataf("C1", meta, "msg %s", "x")
if err.Code() != "C1" {
t.Errorf("expected code 'C1', got '%s'", err.Code())
}
}
func TestNewWithSkipCaller(t *testing.T) {
err := NewWithSkipCaller(1, "skip caller")
if err.Message() != "skip caller" {
t.Errorf("expected 'skip caller', got '%s'", err.Message())
}
}
func TestNewWithSkipCallerAndCode(t *testing.T) {
err := NewWithSkipCallerAndCode(1, "SC01", "msg")
if err.Code() != "SC01" {
t.Errorf("expected 'SC01', got '%s'", err.Code())
}
}
func TestNewWithAll(t *testing.T) {
meta := map[string]any{"a": "b"}
err := NewWithAll(1, "ALL01", meta, "all msg")
if err.Code() != "ALL01" {
t.Errorf("expected 'ALL01', got '%s'", err.Code())
}
if err.Metadata()["a"] != "b" {
t.Error("expected metadata a=b")
}
}
func TestNewWithSkipCallerAndCodef(t *testing.T) {
err := NewWithSkipCallerAndCodef(1, "FMT01", "fmt %s", "val")
if err.Code() != "FMT01" {
t.Errorf("expected 'FMT01', got '%s'", err.Code())
}
if err.Message() != "fmt val" {
t.Errorf("expected 'fmt val', got '%s'", err.Message())
}
}
func TestNewWithSkipCallerf(t *testing.T) {
err := NewWithSkipCallerf(1, "fmt %s", "99")
if err.Message() != "fmt 99" {
t.Errorf("expected 'fmt 99', got '%s'", err.Message())
}
}
func TestNewWithAllf(t *testing.T) {
meta := map[string]any{"z": "w"}
err := NewWithAllf(1, "ALLF", meta, "msg %s", "x")
if err.Code() != "ALLF" {
t.Errorf("expected 'ALLF', got '%s'", err.Code())
}
}
func TestNewWithRawData(t *testing.T) {
err := NewWithRawData("file.go", "10", "myFunc", "RAW01", "raw msg", nil, []byte("stack"))
if err.File() != "file.go" {
t.Errorf("expected 'file.go', got '%s'", err.File())
}
if err.Line() != 10 {
t.Errorf("expected line 10, got %d", err.Line())
}
if err.Func() != "myFunc" {
t.Errorf("expected 'myFunc', got '%s'", err.Func())
}
if err.Code() != "RAW01" {
t.Errorf("expected 'RAW01', got '%s'", err.Code())
}
if err.Message() != "raw msg" {
t.Errorf("expected 'raw msg', got '%s'", err.Message())
}
}
func TestNewAsSlice(t *testing.T) {
errs := NewAsSlice("slice error")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewAsSlicef(t *testing.T) {
errs := NewAsSlicef("error %d", 1)
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithCodeAsSlice(t *testing.T) {
errs := NewWithCodeAsSlice("C1", "msg")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithMetadataAsSlice(t *testing.T) {
errs := NewWithMetadataAsSlice(map[string]any{"k": "v"}, "msg")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithCodeAndMetadataAsSlice(t *testing.T) {
errs := NewWithCodeAndMetadataAsSlice("C1", map[string]any{"k": "v"}, "msg")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithCodeAsSlicef(t *testing.T) {
errs := NewWithCodeAsSlicef("C1", "msg %s", "x")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithMetadataAsSlicef(t *testing.T) {
errs := NewWithMetadataAsSlicef(map[string]any{"k": "v"}, "msg %s", "x")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithCodeAndMetadataAsSlicef(t *testing.T) {
errs := NewWithCodeAndMetadataAsSlicef("C1", map[string]any{"k": "v"}, "msg %s", "x")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithSkipCallerAsSlice(t *testing.T) {
errs := NewWithSkipCallerAsSlice(1, "msg")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithSkipCallerAndCodeAsSlice(t *testing.T) {
errs := NewWithSkipCallerAndCodeAsSlice(1, "C1", "msg")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithAllAsSlice(t *testing.T) {
errs := NewWithAllAsSlice(1, "C1", map[string]any{"k": "v"}, "msg")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithSkipCallerAndCodeAsSlicef(t *testing.T) {
errs := NewWithSkipCallerAndCodeAsSlicef(1, "C1", "msg %s", "x")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithSkipCallerAsSlicef(t *testing.T) {
errs := NewWithSkipCallerAsSlicef(1, "msg %s", "x")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithAllAsSlicef(t *testing.T) {
errs := NewWithAllAsSlicef(1, "C1", map[string]any{"k": "v"}, "msg %s", "x")
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestNewWithRawDataAsSlice(t *testing.T) {
errs := NewWithRawDataAsSlice("f.go", "5", "fn", "C1", "msg", nil, []byte("stack"))
if len(errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(errs))
}
}
func TestTargetWithCode(t *testing.T) {
target := TargetWithCode("T01")
if !target.IsTarget() {
t.Error("expected IsTarget to be true")
}
if target.Code() != "T01" {
t.Errorf("expected code 'T01', got '%s'", target.Code())
}
}
func TestTargetWithMessage(t *testing.T) {
target := TargetWithMessage("target msg")
if !target.IsTarget() {
t.Error("expected IsTarget to be true")
}
if target.Message() != "target msg" {
t.Errorf("expected 'target msg', got '%s'", target.Message())
}
}
func TestErrError(t *testing.T) {
err := New("test error")
s := err.Error()
if s == "" {
t.Error("expected non-empty Error() string")
}
}
func TestErrStackAsSlice(t *testing.T) {
err := New("test error")
stack := err.StackAsSlice()
if len(stack) == 0 {
t.Error("expected non-empty StackAsSlice()")
}
// Verifica que não contém \t ou \n
for _, line := range stack {
if strings.Contains(line, "\t") || strings.Contains(line, "\n") {
t.Errorf("expected StackAsSlice() line to not contain tabs or newlines, got: %s", line)
}
}
}
func TestErrErrorWithCode(t *testing.T) {
err := NewWithCode("C1", "msg")
s := err.Error()
if s == "" {
t.Error("expected non-empty Error() string")
}
}
func TestErrErrorWithMetadata(t *testing.T) {
err := NewWithMetadata(map[string]any{"k": "v"}, "msg")
s := err.Error()
if s == "" {
t.Error("expected non-empty Error() string")
}
}
func TestErrTargetError(t *testing.T) {
target := TargetWithCode("T01")
s := target.Error()
if s == "" {
t.Error("expected non-empty Error() string for target")
}
}
func TestErrTargetErrorWithMessage(t *testing.T) {
target := TargetWithMessage("msg")
s := target.Error()
if s == "" {
t.Error("expected non-empty Error() string for target with message")
}
}
func TestErrCause(t *testing.T) {
err := New("cause test")
cause := err.Cause()
if cause == nil {
t.Error("expected non-nil cause")
}
}
func TestErrSnapshot(t *testing.T) {
err := New("snap")
if err.Snapshot() != "snap" {
t.Errorf("expected 'snap', got '%s'", err.Snapshot())
}
}
func TestErrSnapshotWithCode(t *testing.T) {
err := NewWithCode("C1", "snap")
snap := err.Snapshot()
if snap == "" {
t.Error("expected non-empty snapshot")
}
}
func TestErrString(t *testing.T) {
err := New("string test")
if err.String() != err.Error() {
t.Error("String() should equal Error()")
}
}
func TestErrRaw(t *testing.T) {
err := New("raw test")
raw := err.Raw()
if raw == "" {
t.Error("expected non-empty Raw()")
}
}
func TestErrRawWithCode(t *testing.T) {
err := NewWithCode("C1", "raw test")
raw := err.Raw()
if raw == "" {
t.Error("expected non-empty Raw() with code")
}
}
func TestErrRawWithMetadata(t *testing.T) {
err := NewWithMetadata(map[string]any{"k": "v"}, "raw test")
raw := err.Raw()
if raw == "" {
t.Error("expected non-empty Raw() with metadata")
}
}
func TestErrStack(t *testing.T) {
err := New("stack test")
stack := err.Stack()
if stack == "" {
t.Error("expected non-empty Stack()")
}
}
func TestErrStackFormatted(t *testing.T) {
err := New("stack test")
stack := err.Stack()
if stack == "" {
t.Error("expected non-empty Stack()")
}
}
func TestErrHasMessage(t *testing.T) {
err := New("msg")
if !err.HasMessage() {
t.Error("expected HasMessage to be true")
}
}
func TestSetPolicy(t *testing.T) {
SetPolicy(PolicyDetailed)
if getPolicy() != PolicyDetailed {
t.Error("expected PolicyDetailed")
}
SetPolicy(PolicyNormal)
if getPolicy() != PolicyNormal {
t.Error("expected PolicyNormal")
}
SetPolicy(PolicyNative)
if getPolicy() != PolicyNative {
t.Error("expected PolicyNative")
}
SetPolicy(PolicyNormal) // restore
}
func TestErrWithParent(t *testing.T) {
parent := New("parent error")
child := Inherit(parent, "child error")
s := child.Error()
if s == "" {
t.Error("expected non-empty error with parent")
}
}
func TestErrWithParentFormatted(t *testing.T) {
parent := New("parent error")
child := Inherit(parent, "child error")
s := child.Error()
if s == "" {
t.Error("expected non-empty error with parent")
}
}
func TestNewByParent(t *testing.T) {
parent := New("parent error")
child := NewByParent(parent, "child error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if child.HasCode() {
t.Error("expected no code to be inherited")
}
if child.HasMetadata() {
t.Error("expected no metadata to be inherited")
}
}
func TestNewByParentf(t *testing.T) {
parent := New("parent error")
child := NewByParentf(parent, "child %s", "error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if child.HasCode() {
t.Error("expected no code to be inherited")
}
if child.HasMetadata() {
t.Error("expected no metadata to be inherited")
}
}
func TestNewByParentWithCode(t *testing.T) {
parent := New("parent error")
child := NewByParentWithCode(parent, "ERR_CODE", "child error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if child.HasMetadata() {
t.Error("expected no metadata to be inherited")
}
}
func TestNewByParentWithCodef(t *testing.T) {
parent := New("parent error")
child := NewByParentWithCodef(parent, "ERR_CODE", "child %s", "error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if child.HasMetadata() {
t.Error("expected no metadata to be inherited")
}
}
func TestNewByParentWithMetadata(t *testing.T) {
parent := New("parent error")
metadata := map[string]any{"key": "value"}
child := NewByParentWithMetadata(parent, metadata, "child error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
if child.HasCode() {
t.Error("expected no code to be inherited")
}
}
func TestNewByParentWithMetadataf(t *testing.T) {
parent := New("parent error")
metadata := map[string]any{"key": "value"}
child := NewByParentWithMetadataf(parent, metadata, "child %s", "error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
if child.HasCode() {
t.Error("expected no code to be inherited")
}
}
func TestNewByParentWithCodeAndMetadata(t *testing.T) {
parent := New("parent error")
metadata := map[string]any{"key": "value"}
child := NewByParentWithCodeAndMetadata(parent, "ERR_CODE", metadata, "child error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
}
func TestNewByParentWithCodeAndMetadataf(t *testing.T) {
parent := New("parent error")
metadata := map[string]any{"key": "value"}
child := NewByParentWithCodeAndMetadataf(parent, "ERR_CODE", metadata, "child %s", "error")
if child.parent != parent {
t.Error("expected parent to be set")
}
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
}
func TestNewByChain(t *testing.T) {
errs := []error{New("err1"), New("err2"), New("err3")}
child := NewByChain(errs, "child error")
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if child.ChainLen() != 3 {
t.Errorf("expected chain length 3, got %d", child.ChainLen())
}
if child.HasCode() {
t.Error("expected no code")
}
if child.HasMetadata() {
t.Error("expected no metadata")
}
}
func TestNewByChain_EmptySlice(t *testing.T) {
child := NewByChain(nil, "child error")
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainf(t *testing.T) {
errs := []error{New("err1"), New("err2")}
child := NewByChainf(errs, "child %s", "error")
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if child.ChainLen() != 2 {
t.Errorf("expected chain length 2, got %d", child.ChainLen())
}
if child.HasCode() {
t.Error("expected no code")
}
if child.HasMetadata() {
t.Error("expected no metadata")
}
}
func TestNewByChainf_EmptySlice(t *testing.T) {
child := NewByChainf(nil, "child %s", "error")
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainWithCode(t *testing.T) {
errs := []error{New("err1"), New("err2")}
child := NewByChainWithCode(errs, "ERR_CODE", "child error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if child.ChainLen() != 2 {
t.Errorf("expected chain length 2, got %d", child.ChainLen())
}
if child.HasMetadata() {
t.Error("expected no metadata")
}
}
func TestNewByChainWithCode_EmptySlice(t *testing.T) {
child := NewByChainWithCode(nil, "ERR_CODE", "child error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainWithCodef(t *testing.T) {
errs := []error{New("err1"), New("err2")}
child := NewByChainWithCodef(errs, "ERR_CODE", "child %s", "error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if child.HasMetadata() {
t.Error("expected no metadata")
}
}
func TestNewByChainWithCodef_EmptySlice(t *testing.T) {
child := NewByChainWithCodef(nil, "ERR_CODE", "child %s", "error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainWithMetadata(t *testing.T) {
errs := []error{New("err1"), New("err2")}
metadata := map[string]any{"key": "value"}
child := NewByChainWithMetadata(errs, metadata, "child error")
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
if child.HasCode() {
t.Error("expected no code")
}
}
func TestNewByChainWithMetadata_EmptySlice(t *testing.T) {
metadata := map[string]any{"key": "value"}
child := NewByChainWithMetadata(nil, metadata, "child error")
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainWithMetadataf(t *testing.T) {
errs := []error{New("err1"), New("err2")}
metadata := map[string]any{"key": "value"}
child := NewByChainWithMetadataf(errs, metadata, "child %s", "error")
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
if child.HasCode() {
t.Error("expected no code")
}
}
func TestNewByChainWithMetadataf_EmptySlice(t *testing.T) {
metadata := map[string]any{"key": "value"}
child := NewByChainWithMetadataf(nil, metadata, "child %s", "error")
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainWithCodeAndMetadata(t *testing.T) {
errs := []error{New("err1"), New("err2")}
metadata := map[string]any{"key": "value"}
child := NewByChainWithCodeAndMetadata(errs, "ERR_CODE", metadata, "child error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
}
func TestNewByChainWithCodeAndMetadata_EmptySlice(t *testing.T) {
metadata := map[string]any{"key": "value"}
child := NewByChainWithCodeAndMetadata(nil, "ERR_CODE", metadata, "child error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainWithCodeAndMetadataf(t *testing.T) {
errs := []error{New("err1"), New("err2")}
metadata := map[string]any{"key": "value"}
child := NewByChainWithCodeAndMetadataf(errs, "ERR_CODE", metadata, "child %s", "error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if child.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", child.Message())
}
if !child.HasParent() {
t.Error("expected parent chain to be set")
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", child.Metadata()["key"])
}
}
func TestNewByChainWithCodeAndMetadataf_EmptySlice(t *testing.T) {
metadata := map[string]any{"key": "value"}
child := NewByChainWithCodeAndMetadataf(nil, "ERR_CODE", metadata, "child %s", "error")
if child.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", child.Code())
}
if !child.HasMetadata() {
t.Error("expected metadata to be set")
}
if child.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChain_ChainOrder(t *testing.T) {
err1 := NewWithCode("CODE1", "first")
err2 := NewWithCode("CODE2", "second")
err3 := NewWithCode("CODE3", "third")
child := NewByChain([]error{err1, err2, err3}, "wrapper")
chain := child.Chain()
if len(chain) != 3 {
t.Fatalf("expected 3 parents in chain, got %d", len(chain))
}
if chain[0].Message() != "first" {
t.Errorf("expected first parent message 'first', got '%s'", chain[0].Message())
}
if chain[1].Message() != "second" {
t.Errorf("expected second parent message 'second', got '%s'", chain[1].Message())
}
if chain[2].Message() != "third" {
t.Errorf("expected third parent message 'third', got '%s'", chain[2].Message())
}
}
func TestNewByChain_IsRecognizesChainNode(t *testing.T) {
target := NewWithCode("DEEP_ERR", "deep error")
errs := []error{New("shallow"), target}
child := NewByChain(errs, "wrapper")
if !Is(child, TargetWithCode("DEEP_ERR")) {
t.Error("expected Is to recognize code from chain node")
}
}
func TestNewByChainAsSlice(t *testing.T) {
errs := []error{New("err1"), New("err2")}
result := NewByChainAsSlice(errs, "child error")
if len(result) != 1 {
t.Fatalf("expected slice of length 1, got %d", len(result))
}
var e *Err
if !As(result[0], &e) {
t.Fatal("expected result to be *Err")
}
if e.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", e.Message())
}
if !e.HasParent() {
t.Error("expected parent chain to be set")
}
}
func TestNewByChainAsSlice_EmptySlice(t *testing.T) {
result := NewByChainAsSlice(nil, "child error")
if len(result) != 1 {
t.Fatalf("expected slice of length 1, got %d", len(result))
}
var e *Err
if !As(result[0], &e) {
t.Fatal("expected result to be *Err")
}
if e.HasParent() {
t.Error("expected no parent when slice is empty")
}
}
func TestNewByChainAsSlicef(t *testing.T) {
errs := []error{New("err1"), New("err2")}
result := NewByChainAsSlicef(errs, "child %s", "error")
if len(result) != 1 {
t.Fatalf("expected slice of length 1, got %d", len(result))
}
var e *Err
if !As(result[0], &e) {
t.Fatal("expected result to be *Err")
}
if e.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", e.Message())
}
if !e.HasParent() {
t.Error("expected parent chain to be set")
}
}
func TestNewByChainWithCodeAsSlice(t *testing.T) {
errs := []error{New("err1"), New("err2")}
result := NewByChainWithCodeAsSlice(errs, "ERR_CODE", "child error")
if len(result) != 1 {
t.Fatalf("expected slice of length 1, got %d", len(result))
}
var e *Err
if !As(result[0], &e) {
t.Fatal("expected result to be *Err")
}
if e.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", e.Code())
}
if !e.HasParent() {
t.Error("expected parent chain to be set")
}
}
func TestNewByChainWithCodeAsSlicef(t *testing.T) {
errs := []error{New("err1"), New("err2")}
result := NewByChainWithCodeAsSlicef(errs, "ERR_CODE", "child %s", "error")
if len(result) != 1 {
t.Fatalf("expected slice of length 1, got %d", len(result))
}
var e *Err
if !As(result[0], &e) {
t.Fatal("expected result to be *Err")
}
if e.Code() != "ERR_CODE" {
t.Errorf("expected code 'ERR_CODE', got '%s'", e.Code())
}
if e.Message() != "child error" {
t.Errorf("expected message 'child error', got '%s'", e.Message())
}
if !e.HasParent() {
t.Error("expected parent chain to be set")
}
}
func TestNewByChainWithMetadataAsSlice(t *testing.T) {
errs := []error{New("err1"), New("err2")}
metadata := map[string]any{"key": "value"}
result := NewByChainWithMetadataAsSlice(errs, metadata, "child error")
if len(result) != 1 {
t.Fatalf("expected slice of length 1, got %d", len(result))
}
var e *Err
if !As(result[0], &e) {
t.Fatal("expected result to be *Err")
}
if !e.HasMetadata() {
t.Error("expected metadata to be set")
}
if e.Metadata()["key"] != "value" {
t.Errorf("expected metadata key 'value', got '%v'", e.Metadata()["key"])
}
if !e.HasParent() {
t.Error("expected parent chain to be set")
}
}
func TestNewByChainWithMetadataAsSlicef(t *testing.T) {