Repository navigation
Expand file tree
/
Copy pathtry_catch.go
More file actions
73 lines (66 loc) · 1.44 KB
/
Copy pathtry_catch.go
File metadata and controls
73 lines (66 loc) · 1.44 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
package try
/*
Converts a panic to an error, idempotently adding a stacktrace.
*/
func Catch(fun func()) (err error) {
defer Rec(&err)
if fun != nil {
fun()
}
return
}
/*
Converts a panic to an error, if the error satisfies the provided test.
Otherwise re-panics. Idempotently adds a stacktrace.
*/
func CatchOnly(test func(error) bool, fun func()) (err error) {
defer RecOnly(&err, test)
if fun != nil {
fun()
}
return
}
/*
Shortcut for `Catch() != nil`. Useful when you want to handle all errors while
ignoring their content.
*/
func Caught(fun func()) bool {
return Catch(fun) != nil
}
/*
Shortcut for `CatchOnly() != nil`. Useful when you want to handle a specific
error while ignoring its content.
*/
func CaughtOnly(test func(error) bool, fun func()) bool {
return CatchOnly(test, fun) != nil
}
/*
Runs a function, catching and ignoring ALL panics.
*/
func Ignoring(fun func()) {
defer Ignore()
if fun != nil {
fun()
}
}
/*
Runs a function, catching and ignoring only the panics that satisfy the provided
test. Idempotently adds a stacktrace to all panics.
*/
func IgnoringOnly(test func(error) bool, fun func()) {
defer IgnoreOnly(test)
if fun != nil {
fun()
}
}
/*
Runs a function, "transmuting" the resulting panics by calling the provided
transformer, which may choose to suppress or wrap specific error types. See
`Trans`.
*/
func Transing(trans func(error) error, fun func()) {
defer Trans(trans)
if fun != nil {
fun()
}
}