-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover.go
58 lines (45 loc) · 923 Bytes
/
recover.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
// Package recover contains basic functions
// which helps to work with `recover` in bit pleasant way.
package recover
// All performs call cb function with recovered value.
func All(cb func(v interface{})) {
r := recover()
if r == nil {
return
}
cb(r)
}
// One performs call cb function with recovered value
// in case when recovered value equals to e.
func One(e error, cb func(v interface{})) {
r := recover()
if r == nil {
return
}
if r == e {
cb(r)
return
}
panic(r)
}
// Any performs call cb function with recovered value
// in case when recovered value exists in slice errors.
func Any(errors []error, cb func(v interface{})) {
r := recover()
if r == nil {
return
}
if len(errors) == 0 || inErrors(r, errors) {
cb(r)
return
}
panic(r)
}
func inErrors(e interface{}, errors []error) bool {
for _, err := range errors {
if e == err {
return true
}
}
return false
}