-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrx_test.go
212 lines (155 loc) · 4.3 KB
/
errx_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
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
package errx
import (
"encoding/json"
"errors"
"fmt"
"runtime"
"testing"
)
var _DBErr = errors.New("database_error")
func TestErrx01(t *testing.T) {
// 1.
var e error
e = fmt.Errorf("hello")
e = fmt.Errorf("an error: %w", e)
fmt.Printf("==> a1. %v\n", e)
fmt.Printf("==> a2. %v\n", errors.Unwrap(e))
e1, e2 := errors.New("hello"), errors.New("world")
e = errors.Join(e1, e2)
fmt.Printf("==> a3. %v\n", e)
fmt.Printf("==> a4. %v\n", errors.Unwrap(e))
// 2.
var err *ErrX
// errx = new(ErrX)
err = New(errors.New("wrong"))
err.WithKind("kind42").WithKind("kind_xx").WithCode("code42")
fmt.Printf("==> b1. ErrX: %+#v\n", err)
fmt.Printf("==> b2. errors: %v\n", err.errors)
// 3.
err = fn01ErrX()
fmt.Printf("==> c1. err is nil: %t\n", err == nil)
e = fn02ErrX()
err, _ = e.(*ErrX)
fmt.Printf("==> c2. %t, %t\n", e == nil, err.IsNil())
// false, true, true
err = New(nil)
e = err
fmt.Printf("==> c3. is_nil=%t, e=%v\n", err.IsNil(), e)
// 4.
var bts []byte
err = New(errors.New("e1"))
err.WithErrors(errors.New("e2")).WithKind("kind01").WithCaller()
fmt.Printf("==> d3. ErrX: %v\n", err)
bts, _ = json.Marshal(err)
fmt.Printf("==> d3. json: %s\n", bts)
e = testBizError(errors.New("account not found")).WithMsg("account not exists")
err, _ = FromE(e)
err.WithErrors(errors.New("sorry"), nil)
bts, _ = json.Marshal(err)
fmt.Printf("==> d4. json: %s\n", bts)
fmt.Printf("==> d5. debug: %s\n", err)
}
func fn01ErrX() (err *ErrX) {
return nil
}
func fn02ErrX() (e error) {
return fn01ErrX()
}
func testBizError(e error) (err *ErrX) {
return New(e).WithCaller(2).WithKind("biz_error").WithCode("NotFound")
}
func TestErrx02(t *testing.T) {
var (
err1, err2 *ErrX
bts []byte
)
err1 = Eee(Code("NotFound")).WithKind("biz_error").WithCaller()
fmt.Printf("==> 1. err1: %+#v\n", err1)
err2 = New(errors.New("an error"), Code("DBError")).WithKind("internal_error").WithCaller()
err1.WithErrors(err2)
fmt.Printf("==> 2. err1: %+#v\n", err1)
bts, _ = json.Marshal(err1)
fmt.Printf("==> 3. err1 json: %s\n", bts)
fmt.Printf("==> 4a. err1 debug: %s\n", err1)
fmt.Printf("==> 4b. err1 debug: %+v\n", err1)
err1 = nil
bts, _ = json.Marshal(err1)
fmt.Printf("==> 5. err1 json: %s\n", bts)
fmt.Printf("==> 6. err1 debug: %s\n", err1)
}
func TestErr03(t *testing.T) {
var eE Error
eE = Eee()
fmt.Printf("==> 1. Error: %t, %v\n", eE.IsNil(), eE)
eE = New(nil)
fmt.Printf("==> 2. Error: %t, %v\n", eE.IsNil(), eE)
}
type _Err1 struct {
str string
}
func (self _Err1) Error() string {
return self.str
}
func TestErr04(t *testing.T) {
err1 := _Err1{str: "_Err1"}
// err2 := _Err1{str: "_Err2"}
var e1 error
e1 = err1
var e2 _Err1
fmt.Printf("==> 1. %t\n", errors.Is(e1, e2)) // false
e1 = fmt.Errorf("...: %w", err1)
fmt.Printf("==> 2. %t\n", errors.Is(e1, e2)) // false
e2 = _Err1{str: "_Err1"}
fmt.Printf("==> 3. %t\n", errors.Is(e1, e2)) // true
var e3 _Err1
fmt.Printf("==> 4. %t\n", errors.As(e1, &e3)) // true
fmt.Printf("==> 5. %v\n", e3)
e1 = errors.Join(err1, errors.New("an_error"))
fmt.Printf("==> 6. %t\n", errors.As(e1, &e3)) // true
fmt.Printf("==> 7. %v\n", e3)
var e4 _Err1
e1 = errors.Join(errors.New("an_error"), err1)
fmt.Printf("==> 8. %t\n", errors.As(e1, &e4)) // true
fmt.Printf("==> 9. %v\n", e4)
}
func TestCaller(t *testing.T) {
_, file, line, ok := runtime.Caller(-1)
fmt.Println("~~~", file, line, ok)
}
func TestErr05(t *testing.T) {
var errx = newDBErr(fmt.Errorf("...")).WithCaller()
fmt.Printf("==> 1. %v\n", errx)
fmt.Printf("==> 2. %t\n", errx.Is(_DBErr))
}
func newDBErr(e error) *ErrX {
return New(_DBErr).WithErrors(e).WithDefault("internal_error", "InternalError", "")
// WithKind("internal_error").
// WithCode("InternalError")
}
func TestJSON(t *testing.T) {
var (
e error
bts []byte
)
e = errors.New("...")
bts, _ = json.Marshal(e)
fmt.Printf("==> 1. %s\n", bts)
e = fmt.Errorf("...")
bts, _ = json.Marshal(e)
fmt.Printf("==> 2. %s\n", bts)
bts, _ = json.Marshal(&e)
fmt.Printf("==> 3. %s\n", bts)
bts, _ = json.Marshal(e.Error())
fmt.Printf("==> 4. %s\n", bts)
e = testError{E: "an error"}
bts, _ = json.Marshal(e)
fmt.Printf("==> 5. %s\n", bts)
bts, _ = json.Marshal(&e)
fmt.Printf("==> 6. %s\n", bts)
}
type testError struct {
E string
}
func (self testError) Error() string {
return self.E
}