-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.go
55 lines (48 loc) · 1.38 KB
/
panic.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
package kerror
import "fmt"
// Nil triggers a panic with a stack trace by reason of an unacceptable operation on nil.
func Nil() {
panic(newErrorWithTrace(2, ENil, "unacceptable operation on nil", nil))
}
// Panic triggers a panic with a stack trace and the given message.
func Panic(message string) {
panic(newErrorWithTrace(2, EPanic, message, nil))
}
// Panicf is a variant of the Panic with message formatting.
func Panicf(format string, a ...interface{}) {
panic(newErrorWithTrace(2, EPanic, fmt.Sprintf(format, a...), nil))
}
// Recovered casts the given untyped result of the panic recovering to error.
// If the argument v is nil then nil will be returned. Otherwise error will be
// returned (is v doesn't implement the error interface it will be transformed
// to the native error).
func Recovered(v interface{}) error {
if v == nil {
return nil
}
if err, ok := v.(error); ok {
return err
}
return fmt.Errorf("%+v", v)
}
// Try calls f, recovers a panic if occurred and returns it as error.
func Try(f func() error) (err error) {
defer Catch(&err)
return f()
}
// Catch recovers a panic if occurred and saves it to err.
func Catch(err *error) {
if err == nil {
return
}
if v := recover(); v != nil {
coerr := NewCollector()
coerr.Collect(*err)
if e, ok := v.(error); ok {
coerr.Collect(e)
} else {
coerr.Collect(fmt.Errorf("%+v", v))
}
*err = coerr.Error()
}
}