-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_example_test.go
429 lines (353 loc) · 8.18 KB
/
errors_example_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
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
package lo
import (
"fmt"
)
func ExampleValidate() {
i := 42
err1 := Validate(i < 0, "expected %d < 0", i)
err2 := Validate(i > 0, "expected %d > 0", i)
fmt.Printf("%v\n%v", err1, err2)
// Output:
// expected 42 < 0
// <nil>
}
func ExampleMust() {
defer func() {
_ = recover()
}()
// won't panic
Must(42, nil)
// won't panic
cb := func() (int, error) {
return 42, nil
}
Must(cb())
// will panic
Must(42, fmt.Errorf("my error"))
// will panic with error message
Must(42, fmt.Errorf("world"), "hello")
}
func ExampleMust0() {
defer func() {
_ = recover()
}()
// won't panic
Must0(nil)
// will panic
Must0(fmt.Errorf("my error"))
// will panic with error message
Must0(fmt.Errorf("world"), "hello")
}
func ExampleMust1() {
defer func() {
_ = recover()
}()
// won't panic
Must1(42, nil)
// won't panic
cb := func() (int, error) {
return 42, nil
}
Must1(cb())
// will panic
Must1(42, fmt.Errorf("my error"))
// will panic with error message
Must1(42, fmt.Errorf("world"), "hello")
}
func ExampleMust2() {
defer func() {
_ = recover()
}()
// won't panic
Must2(42, "hello", nil)
// will panic
Must2(42, "hello", fmt.Errorf("my error"))
// will panic with error message
Must2(42, "hello", fmt.Errorf("world"), "hello")
}
func ExampleMust3() {
defer func() {
_ = recover()
}()
// won't panic
Must3(42, "hello", 4.2, nil)
// will panic
Must3(42, "hello", 4.2, fmt.Errorf("my error"))
// will panic with error message
Must3(42, "hello", 4.2, fmt.Errorf("world"), "hello")
}
func ExampleMust4() {
defer func() {
_ = recover()
}()
// won't panic
Must4(42, "hello", 4.2, true, nil)
// will panic
Must4(42, "hello", 4.2, true, fmt.Errorf("my error"))
// will panic with error message
Must4(42, "hello", 4.2, true, fmt.Errorf("world"), "hello")
}
func ExampleMust5() {
defer func() {
_ = recover()
}()
// won't panic
Must5(42, "hello", 4.2, true, foo{}, nil)
// will panic
Must5(42, "hello", 4.2, true, foo{}, fmt.Errorf("my error"))
// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, fmt.Errorf("world"), "hello")
}
func ExampleMust6() {
defer func() {
_ = recover()
}()
// won't panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", nil)
// will panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", fmt.Errorf("my error"))
// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, "foobar", fmt.Errorf("world"), "hello")
}
func ExampleTry() {
ok1 := Try(func() error {
return nil
})
ok2 := Try(func() error {
return fmt.Errorf("my error")
})
ok3 := Try(func() error {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry1() {
ok1 := Try1(func() error {
return nil
})
ok2 := Try1(func() error {
return fmt.Errorf("my error")
})
ok3 := Try1(func() error {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry2() {
ok1 := Try2(func() (int, error) {
return 42, nil
})
ok2 := Try2(func() (int, error) {
return 42, fmt.Errorf("my error")
})
ok3 := Try2(func() (int, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry3() {
ok1 := Try3(func() (int, string, error) {
return 42, "foobar", nil
})
ok2 := Try3(func() (int, string, error) {
return 42, "foobar", fmt.Errorf("my error")
})
ok3 := Try3(func() (int, string, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry4() {
ok1 := Try4(func() (int, string, float64, error) {
return 42, "foobar", 4.2, nil
})
ok2 := Try4(func() (int, string, float64, error) {
return 42, "foobar", 4.2, fmt.Errorf("my error")
})
ok3 := Try4(func() (int, string, float64, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry5() {
ok1 := Try5(func() (int, string, float64, bool, error) {
return 42, "foobar", 4.2, true, nil
})
ok2 := Try5(func() (int, string, float64, bool, error) {
return 42, "foobar", 4.2, true, fmt.Errorf("my error")
})
ok3 := Try5(func() (int, string, float64, bool, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry6() {
ok1 := Try6(func() (int, string, float64, bool, foo, error) {
return 42, "foobar", 4.2, true, foo{}, nil
})
ok2 := Try6(func() (int, string, float64, bool, foo, error) {
return 42, "foobar", 4.2, true, foo{}, fmt.Errorf("my error")
})
ok3 := Try6(func() (int, string, float64, bool, foo, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTryOr() {
value1, ok1 := TryOr(func() (int, error) {
return 42, nil
}, 21)
value2, ok2 := TryOr(func() (int, error) {
return 42, fmt.Errorf("my error")
}, 21)
value3, ok3 := TryOr(func() (int, error) {
panic("my error")
}, 21)
fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
// Output:
// 42 true
// 21 false
// 21 false
}
func ExampleTryOr1() {
value1, ok1 := TryOr1(func() (int, error) {
return 42, nil
}, 21)
value2, ok2 := TryOr1(func() (int, error) {
return 42, fmt.Errorf("my error")
}, 21)
value3, ok3 := TryOr1(func() (int, error) {
panic("my error")
}, 21)
fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
// Output:
// 42 true
// 21 false
// 21 false
}
func ExampleTryOr2() {
value1, value2, ok3 := TryOr2(func() (int, string, error) {
panic("my error")
}, 21, "hello")
fmt.Printf("%v %v %v\n", value1, value2, ok3)
// Output: 21 hello false
}
func ExampleTryOr3() {
value1, value2, value3, ok3 := TryOr3(func() (int, string, bool, error) {
panic("my error")
}, 21, "hello", false)
fmt.Printf("%v %v %v %v\n", value1, value2, value3, ok3)
// Output: 21 hello false false
}
func ExampleTryOr4() {
value1, value2, value3, value4, ok3 := TryOr4(func() (int, string, bool, foo, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"})
fmt.Printf("%v %v %v %v %v\n", value1, value2, value3, value4, ok3)
// Output: 21 hello false {bar} false
}
func ExampleTryOr5() {
value1, value2, value3, value4, value5, ok3 := TryOr5(func() (int, string, bool, foo, float64, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2)
fmt.Printf("%v %v %v %v %v %v\n", value1, value2, value3, value4, value5, ok3)
// Output: 21 hello false {bar} 4.2 false
}
func ExampleTryOr6() {
value1, value2, value3, value4, value5, value6, ok3 := TryOr6(func() (int, string, bool, foo, float64, string, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2, "world")
fmt.Printf("%v %v %v %v %v %v %v\n", value1, value2, value3, value4, value5, value6, ok3)
// Output: 21 hello false {bar} 4.2 world false
}
func ExampleTryWithErrorValue() {
err1, ok1 := TryWithErrorValue(func() error {
return nil
})
err2, ok2 := TryWithErrorValue(func() error {
return fmt.Errorf("my error")
})
err3, ok3 := TryWithErrorValue(func() error {
panic("my error")
})
fmt.Printf("%v %v\n", err1, ok1)
fmt.Printf("%v %v\n", err2, ok2)
fmt.Printf("%v %v\n", err3, ok3)
// Output:
// <nil> true
// my error false
// my error false
}
func ExampleTryCatchWithErrorValue() {
TryCatchWithErrorValue(
func() error {
panic("trigger an error")
},
func(err any) {
fmt.Printf("catch: %s", err)
},
)
// Output: catch: trigger an error
}
type myError struct{}
func (e myError) Error() string {
return "my error"
}
func ExampleErrorsAs() {
doSomething := func() error {
return &myError{}
}
err := doSomething()
if rateLimitErr, ok := ErrorsAs[*myError](err); ok {
fmt.Printf("is type myError, err: %s", rateLimitErr.Error())
} else {
fmt.Printf("is not type myError")
}
// Output: is type myError, err: my error
}