-
Notifications
You must be signed in to change notification settings - Fork 6
/
recover.go
62 lines (57 loc) · 1.3 KB
/
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
59
60
61
62
package grace
import (
"runtime/debug"
)
type Recovery struct {
err *error
value string
stack []byte
}
// Error set the error pointer to error message only
func (rc *Recovery) Error() {
if r := recover(); r != nil {
switch err := (*rc.err).(type) {
case *grace:
g := &grace{
handlers: err.handlers,
errors: []error{New(r)},
}
err.errors = append(err.errors, Handler(g).Loop())
default:
*rc.err = &grace{errors: []error{New(r)}}
}
}
}
// Stack set the error pointer to stack trace only
func (rc *Recovery) Stack() {
if r := recover(); r != nil {
switch err := (*rc.err).(type) {
case *grace:
g := &grace{
handlers: err.handlers,
errors: []error{New(string(debug.Stack()))},
}
err.errors = append(err.errors, Handler(g).Loop())
default:
*rc.err = &grace{errors: []error{New(string(debug.Stack()))}}
}
}
}
// Recover set an error pointer to recover error with stack trace.
func Recover(e *error) *Recovery {
if r := recover(); r != nil {
switch err := (*e).(type) {
case *grace:
g := &grace{
handlers: err.handlers,
errors: []error{New(r, string(debug.Stack()))},
}
err.errors = append(err.errors, Handler(g).Loop())
default:
*e = &grace{errors: []error{New(r, string(debug.Stack()))}}
}
}
return &Recovery{
err: e,
}
}