-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
247 lines (214 loc) · 6.55 KB
/
example_test.go
File metadata and controls
247 lines (214 loc) · 6.55 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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xerr/blob/main/LICENSE.
package xerr_test
import (
"errors"
"fmt"
"io"
"os"
"sync"
"github.com/actforgood/xerr"
)
func ExampleNew() {
err := xerr.New("something went bad")
fmt.Println(err.Error())
// Example output:
// something went bad
fmt.Printf("%+v\n", err)
// Example output:
// something went bad
// github.com/actforgood/xerr_test.ExampleNew
// /Users/bogdan/work/go/xerr/example_test.go:15
// testing.runExample
// /usr/local/go/src/testing/run_example.go:63
// testing.runExamples
// /usr/local/go/src/testing/example.go:44
// testing.(*M).Run
// /usr/local/go/src/testing/testing.go:1418
// main.main
// _testmain.go:79
// runtime.main
// /usr/local/go/src/runtime/proc.go:225
// runtime.goexit
// /usr/local/go/src/runtime/asm_amd64.s:1371
}
func ExampleErrorf() {
err := xerr.Errorf("something went bad with %s", "this example")
fmt.Println(err.Error())
// Example output:
// something went bad with this example
fmt.Printf("%+v\n", err)
// Example output:
// something went bad with this example
// github.com/actforgood/xerr_test.ExampleErrorf
// /Users/bogdan/work/go/xerr/example_test.go:44
// testing.runExample
// /usr/local/go/src/testing/run_example.go:63
// testing.runExamples
// /usr/local/go/src/testing/example.go:44
// testing.(*M).Run
// /usr/local/go/src/testing/testing.go:1418
// main.main
// _testmain.go:83
// runtime.main
// /usr/local/go/src/runtime/proc.go:225
// runtime.goexit
// /usr/local/go/src/runtime/asm_amd64.s:1371
}
// DoSomeOperation simulates a function which (may) return an error.
func DoSomeOperation() error {
return errors.New("op err")
}
// DoSomeOtherOperation simulates a function which (may) return an error.
func DoSomeOtherOperation() error {
return xerr.New("op err")
}
func ExampleWrap_withStandardError() {
err := DoSomeOperation()
if err != nil {
err = xerr.Wrap(err, "could not perform operation")
}
fmt.Println(err.Error())
// Example output:
// could not perform operation: op err
fmt.Printf("%+v\n", err)
// Example output:
// could not perform operation: op err
// github.com/actforgood/xerr_test.ExampleWrap_withStandardError
// /Users/bogdan/work/go/xerr/example_test.go:54
// testing.runExample
// /usr/local/go/src/testing/run_example.go:63
// testing.runExamples
// /usr/local/go/src/testing/example.go:44
// testing.(*M).Run
// /usr/local/go/src/testing/testing.go:1418
// main.main
// _testmain.go:81
// runtime.main
// /usr/local/go/src/runtime/proc.go:225
// runtime.goexit
// /usr/local/go/src/runtime/asm_amd64.s:1371
}
func ExampleWrap_withStackError() {
err := DoSomeOtherOperation()
if err != nil {
err = xerr.Wrap(err, "could not perform operation")
}
fmt.Println(err.Error())
// Example output:
// could not perform operation: op err
fmt.Printf("%+v\n", err)
// Example output:
// could not perform operation: op err
// github.com/actforgood/xerr_test.ExampleWrap_withStackError
// /Users/bogdan/work/go/xerr/example_test.go:83
// github.com/actforgood/xerr_test.DoSomeOtherOperation
// /Users/bogdan/work/go/xerr/example_test.go:48
// github.com/actforgood/xerr_test.ExampleWrap_withStackError
// /Users/bogdan/work/go/xerr/example_test.go:81
// testing.runExample
// /usr/local/go/src/testing/run_example.go:63
// testing.runExamples
// /usr/local/go/src/testing/example.go:44
// testing.(*M).Run
// /usr/local/go/src/testing/testing.go:1418
// main.main
// _testmain.go:79
// runtime.main
// /usr/local/go/src/runtime/proc.go:225
// runtime.goexit
// /usr/local/go/src/runtime/asm_amd64.s:1371
}
func ExampleMultiError_Add_sequential() {
files := []string{
"/this/file/does/not/exist/1",
"/this/file/does/not/exist/2",
"/this/file/does/not/exist/3",
}
var multiErr *xerr.MultiError // save allocation if Add is never called.
for _, file := range files {
if _, err := os.Open(file); err != nil {
multiErr = multiErr.Add(err) // store allocated multiErr.
}
// else do something with that file ...
}
returnErr := multiErr.ErrOrNil()
fmt.Println(returnErr)
// Example of output: note - on Windows the message is slightly different (The system cannot find the path specified)
// error #1
// open /this/file/does/not/exist/1: no such file or directory
// error #2
// open /this/file/does/not/exist/2: no such file or directory
// error #3
// open /this/file/does/not/exist/3: no such file or directory
}
func ExampleMultiError_Add_parallel() {
files := []string{
"/this/file/does/not/exist/1",
"/this/file/does/not/exist/2",
"/this/file/does/not/exist/3",
}
multiErr := xerr.NewMultiError() // we need instance already initialized.
var wg sync.WaitGroup
for _, file := range files {
wg.Go(func() {
if _, err := os.Open(file); err != nil {
_ = multiErr.Add(err) // we can dismiss returned value as multiErr is already initialized.
}
// else do something with that file ...
})
}
wg.Wait()
returnErr := multiErr.ErrOrNil()
fmt.Println(returnErr)
// Example of unordered output:
// error #1
// open /this/file/does/not/exist/3: no such file or directory
// error #2
// open /this/file/does/not/exist/1: no such file or directory
// error #3
// open /this/file/does/not/exist/2: no such file or directory
}
func ExampleMultiError_AddOnce() {
// different errors we want to add to MultiError
err1 := os.ErrNotExist
err2 := io.ErrUnexpectedEOF
err3 := os.ErrNotExist
var multiErr *xerr.MultiError
multiErr = multiErr.AddOnce(err1)
multiErr = multiErr.AddOnce(err2)
multiErr = multiErr.AddOnce(err3) // err3 is the same with err1, so it should be ignored
returnErr := multiErr.ErrOrNil()
fmt.Println(returnErr)
// Output:
// error #1
// file does not exist
// error #2
// unexpected EOF
}
func ExampleMultiError_Errors() {
multiErr := xerr.NewMultiError()
_ = multiErr.Add(errors.New("1st error"))
_ = multiErr.Add(errors.New("2nd error"))
for _, err := range multiErr.Errors() {
fmt.Println(err)
}
// Output:
// 1st error
// 2nd error
}
func ExampleMultiError_Is() {
multiErr := xerr.NewMultiError()
_ = multiErr.Add(io.ErrUnexpectedEOF)
someErrWithStack := xerr.New("stack err")
_ = multiErr.Add(someErrWithStack)
fmt.Println(errors.Is(multiErr, io.ErrUnexpectedEOF))
fmt.Println(errors.Is(multiErr, someErrWithStack))
fmt.Println(errors.Is(multiErr, os.ErrClosed))
// Output:
// true
// true
// false
}