-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
324 lines (279 loc) · 9.12 KB
/
examples_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
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
package eris_test
import (
"encoding/json"
"fmt"
"io"
"testing"
"github.com/risingwavelabs/eris"
)
var (
ErrUnexpectedEOF = eris.New("unexpected EOF").WithCode(eris.CodeUnknown)
FormattedErrUnexpectedEOF = eris.Errorf("unexpected %v", "EOF").WithCode(eris.CodeUnknown)
)
// Demonstrates JSON formatting of wrapped errors that originate from external (non-eris) error
// types.
func ExampleToJSON_external() {
// example func that returns an IO error
readFile := func(fname string) error {
return io.ErrUnexpectedEOF
}
// unpack and print the error
err := readFile("example.json")
u, _ := json.Marshal(eris.ToJSON(err, false)) // false: omit stack trace
fmt.Println(string(u))
// example output:
// {
// "external":"unexpected EOF"
// }
}
// Hack to run examples that don't have a predictable output (i.e. all examples that involve
// printing stack traces).
func TestExampleToJSON_external(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleToJSON_external()
}
// Demonstrates JSON formatting of wrapped errors that originate from global root errors.
func ExampleToJSON_global() {
// example func that wraps a global error value
readFile := func(fname string) error {
return eris.WithCode(eris.Wrapf(ErrUnexpectedEOF, "error reading file '%v'", fname), eris.CodeUnknown)
}
// example func that catches and returns an error without modification
parseFile := func(fname string) error {
// read the file
err := readFile(fname) // line 12
if err != nil {
return err
}
return nil
}
// unpack and print the error via uerr.ToJSON(...)
err := parseFile("example.json") // line 20
u, _ := json.MarshalIndent(eris.ToJSON(err, true), "", "\t") // true: include stack trace
fmt.Printf("%v\n", string(u))
// {
// "root": {
// "code": "unknown",
// "message": "unexpected EOF",
// "stack": [
// "main.main:.../example/main.go:20",
// "main.parseFile:.../example/main.go:12",
// "main.readFile:.../example/main.go:6"
// ]
// },
// "wrap": [
// {
// "code": "unknown",
// "message": "error reading file 'example.json'",
// "stack": "main.readFile:.../example/main.go:6"
// }
// ]
// }
}
func TestExampleToJSON_global(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleToJSON_global()
}
// Demonstrates JSON formatting of wrapped errors that originate from local root errors (created at
// the source of the error via eris.New).
func ExampleToJSON_local() {
// example func that returns an eris error
readFile := func(fname string) error {
return eris.New("unexpected EOF").WithCode(eris.CodeUnknown) // line 3
}
// example func that catches an error and wraps it with additional context
parseFile := func(fname string) error {
// read the file
err := readFile(fname) // line 9
if err != nil {
return eris.WithCode(eris.Wrapf(err, "error reading file '%v'", fname), eris.CodeUnknown)
}
return nil
}
// example func that just catches and returns an error
processFile := func(fname string) error {
// parse the file
err := parseFile(fname) // line 19
if err != nil {
return err
}
return nil
}
// another example func that catches and wraps an error
printFile := func(fname string) error {
// process the file
err := processFile(fname) // line 29
if err != nil {
return eris.WithCode(eris.Wrapf(err, "error printing file '%v'", fname), eris.CodeUnknown)
}
return nil
}
// unpack and print the raw error
err := printFile("example.json") // line 37
u, _ := json.MarshalIndent(eris.ToJSON(err, true), "", "\t")
fmt.Printf("%v\n", string(u))
// example output:
// {
// "root": {
// "code": "unknown",
// "message": "unexpected EOF",
// "stack": [
// "main.main:.../example/main.go:37",
// "main.printFile:.../example/main.go:31",
// "main.printFile:.../example/main.go:29",
// "main.processFile:.../example/main.go:19",
// "main.parseFile:.../example/main.go:11",
// "main.parseFile:.../example/main.go:9",
// "main.readFile:.../example/main.go:3"
// ]
// },
// "wrap": [
// {
// "code": "unknown",
// "message": "error printing file 'example.json'",
// "message": "error printing file 'example.json'",
// },
// {
// "code": "unknown",
// "message": "error reading file 'example.json'",
// "stack": "main.parseFile: .../example/main.go: 11"
// }
// ]
// }
}
func TestExampleToJSON_local(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleToJSON_local()
}
// Demonstrates string formatting of wrapped errors that originate from external (non-eris) error
// types.
func ExampleToString_external() {
// example func that returns an IO error
readFile := func(fname string) error {
return io.ErrUnexpectedEOF
}
// unpack and print the error
err := readFile("example.json")
fmt.Println(eris.ToString(err, false)) // false: omit stack trace
// example output:
// unexpected EOF
}
func TestExampleToString_external(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleToString_external()
}
// Demonstrates string formatting of wrapped errors that originate from global root errors.
func ExampleToString_global() {
// example func that wraps a global error value
readFile := func(fname string) error {
return eris.WithProperty(eris.Wrapf(FormattedErrUnexpectedEOF, "error reading file '%v'", fname), "file", fname)
}
// example func that catches and returns an error without modification
parseFile := func(fname string) error {
// read the file
err := readFile(fname) // line 12
if err != nil {
return err
}
return nil
}
// example func that just catches and returns an error
processFile := func(fname string) error {
// parse the file
err := parseFile(fname) // line 22
if err != nil {
return eris.WithCode(eris.Wrapf(err, "error processing file '%v'", fname), eris.CodeDeadlineExceeded)
}
return nil
}
// call processFile and catch the error
err := processFile("example.json") // line 30
// print the error via fmt.Printf
fmt.Printf("%v\n", err) // %v: omit stack trace
// example output:
// unexpected EOF: error reading file 'example.json'
// unpack and print the error via uerr.ToString(...)
fmt.Printf("%v\n", eris.ToString(err, true)) // true: include stack trace
// code(deadline exceeded) error processing file 'example.json': code(internal) KVs(map[file:example.json]) error reading file 'example.json': code(unknown) unexpected EOF
// code(deadline exceeded) error processing file 'example.json'
// main.readFile:.../example/main.go:6
// code(internal) KVs(map[file:example.json]) error reading file 'example.json'
// main.readFunc:.../example/main.go:1
// code(unknown) unexpected EOF
// main.main:.../example/main.go:30
// main.processFile:.../example/main.go:24
// main.processFile:.../example/main.go:22
// main.parseFile:.../example/main.go:12
// main.readFile:.../example/main.go:6
}
func TestExampleToString_global(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleToString_global()
}
// Demonstrates string formatting of wrapped errors that originate from local root errors (created
// at the source of the error via eris.New).
func ExampleToString_local() {
// example func that returns an eris error
readFile := func(fname string) error {
return eris.New("unexpected EOF").WithCode(eris.CodeAborted) // line 3
}
// example func that catches an error and wraps it with additional context
parseFile := func(fname string) error {
// read the file
err := readFile(fname) // line 9
if err != nil {
return eris.WithProperty(eris.Wrapf(err, "error reading file '%v'", fname), "foo", "bar")
}
return nil
}
// call parseFile and catch the error
err := parseFile("example.json") // line 17
// print the error via fmt.Printf
fmt.Printf("%v\n", err) // %v: omit stack trace
// example output:
// unexpected EOF: error reading file 'example.json'
// unpack and print the error via uerr.ToString(...)
fmt.Println(eris.ToString(err, true)) // true: include stack trace
// code(internal) KVs(map[foo:bar]) error reading file 'example.json': code(aborted) unexpected EOF
// code(internal) KVs(map[foo:bar]) error reading file 'example.json'
// main.parseFile:.../example/main.go:11
// code(aborted) unexpected EOF
// main.main:.../example/main.go:17
// main.parseFile:.../example/main.go:11
// main.parseFile:.../example/main.go:9
// main.readFile:.../example/main.go:3
}
func TestExampleToString_local(t *testing.T) {
if !testing.Verbose() {
return
}
ExampleToString_local()
}
func f() error {
return eris.New("error message").WithCode(eris.CodeDeadlineExceeded)
}
func g() error {
return eris.WithCode(eris.Wrap(f(), "in function g"), eris.CodeCanceled)
}
func h() error {
return eris.WithCode(eris.Wrap(g(), "in function h"), eris.CodeAborted)
}
func TestMainFunc(t *testing.T) {
err := h()
formattedJSON := eris.ToJSON(err, true)
b, _ := json.Marshal(formattedJSON)
fmt.Println(string(b))
// b, _ = json.Marshal(eris.ToJSON(f(), true))
// fmt.Println(string(b))
// panic("tmp")
}