-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdef_errors_test.go
122 lines (107 loc) · 2.82 KB
/
def_errors_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
// Copyright © 2020 Hedzr Yeh.
package cmdr
import (
"testing"
"gopkg.in/hedzr/errors.v3"
)
func TestErrors(t *testing.T) {
a := &ErrorForCmdr{
Ignorable: false,
causer: nil,
msg: "test error 1",
liveArgs: []interface{}{2},
}
if _, ignorable, ok := UnwrapError(a); ok && !ignorable {
// ok
} else {
t.Fatal(a)
}
for _, err := range []error{
ErrShouldBeStopException, ErrBadArg, errWrongEnumValue,
} {
if _, _, ok := UnwrapError(err); ok {
// ok
} else {
t.Fatal(err)
}
}
// for _, err := range []error{
// ErrShouldBeStopException,
// AttachErrorsTo(AttachLiveArgsTo(SetAsAnIgnorableError(NewErrorForCmdr("*ErrorForCmdr here"), true), []int{2, 5}, "bad"), io.EOF),
// } {
// if IsIgnorableError(err) {
// // ok
// liveArgs, e, ok := UnwrapLiveArgsFromCmdrError(err)
// t.Logf("liveArgs: %v, e: %v, ok: %v", liveArgs, e, ok)
// //t.Logf("Unwrap() returned: %v", e.Unwrap())
//
// errs := UnwrapInnerErrorsFromCmdrError(err)
// t.Logf("inner errors are: %v", errs)
// } else {
// t.Fatal(err)
// }
// }
//
// // 1.
// ec := errors.NewContainer("container of errors")
// for _, e := range []error{io.EOF, io.ErrClosedPipe} {
// ec.Attach(e)
// }
// errs := UnwrapInnerErrorsFromCmdrError(&ew{(*errors.WithCauses)(ec)})
// t.Logf("inner errors are: %v, error is: %v", errs, ec.Error())
//
// // 2.
// ewc := errors.WithCause(io.EOF, "*withCauses object here")
// if e1 := AttachErrorsTo(ewc, io.EOF, io.ErrShortBuffer); e1 != nil {
// errs = UnwrapInnerErrorsFromCmdrError(e1)
// t.Logf("inner errors are: %v, error is: %v", errs, e1)
// }
for _, err := range []error{
ErrBadArg, errWrongEnumValue,
} {
if !IsIgnorableError(err) {
// ok
} else {
t.Fatal(err)
}
}
}
type ew struct { //nolint:unused //for test
msg string
// *errors.WithCauses
}
func (e *ew) Error() string { //nolint:unused //for test
return e.msg // e.WithCauses.Error().Error()
}
func TestErrorForCmdr(t *testing.T) {
a := &ErrorForCmdr{
Ignorable: false,
causer: nil,
msg: "test error 1",
liveArgs: []interface{}{2},
}
t.Logf("a is: %v, %T", a, a)
e := newError(false, a)
if _, ok := e.(*errors.WithStackInfo); !ok { //nolint:errorlint //for test
t.Fatal(e)
}
e1 := newErr("test error %d", 2)
// if _, ok := e1.(*errors.WithStackInfo); !ok {
// t.Fatal(e1)
// }
if _, ok := e.(*errors.WithStackInfo); ok { //nolint:errorlint //for test
var et *errors.WithStackInfo
if !errors.As(e, &et) {
t.Fatal("cannot errors.As(e, -> *errors.WithStackInfo)")
}
} else {
t.Fatalf("Is detection failed: e = %+v", e)
}
errors.Is(e, nil)
t.Logf("e has Causer: %v / %v | unwrapped: %v | ",
e.(*errors.WithStackInfo).Cause(), //nolint:errorlint //for test
e.Error(), // errors.Cause(e),
errors.Unwrap(e),
)
t.Logf("e1 is: %v, %T", e1, e1)
}