This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xerror_recovery.go
122 lines (100 loc) · 1.66 KB
/
xerror_recovery.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
package xerror
import (
"os"
"testing"
"github.com/pubgo/xerror/internal/utils"
"github.com/pubgo/xerror/xerror_conf"
)
func RecoverErr(gErr *error, fns ...func(err XErr) XErr) {
val := recover()
if val == nil {
return
}
var err error
handleRecover(&err, val)
if isErrNil(err) {
return
}
err1 := handle(err)
if len(fns) > 0 {
*gErr = fns[0](err1)
return
}
*gErr = err1
}
func RecoverAndRaise(fns ...func(err XErr) XErr) {
val := recover()
if val == nil {
return
}
var err error
handleRecover(&err, val)
if isErrNil(err) {
return
}
err1 := handle(err)
if len(fns) > 0 {
panic(fns[0](err1))
}
panic(err1)
}
func Recovery(fn func(err XErr)) {
Assert(fn == nil, "[fn] should not be nil")
val := recover()
if val == nil {
return
}
var err error
handleRecover(&err, val)
if isErrNil(err) {
return
}
fn(handle(err))
}
func RecoverAndExit() {
val := recover()
if val == nil {
return
}
var err error
handleRecover(&err, val)
if isErrNil(err) {
return
}
err1 := handle(err)
for i := 0; ; i++ {
var cc = utils.CallerWithDepth(xerror_conf.Conf.CallDepth + i)
if cc == "" {
break
}
err1.Caller = append(err1.Caller, cc)
}
p(err1.debugString())
printStack()
os.Exit(1)
}
func RecoverTest(t *testing.T, debugs ...bool) {
val := recover()
if val == nil {
return
}
var err error
handleRecover(&err, val)
if isErrNil(err) {
return
}
err1 := handle(err)
for i := 0; ; i++ {
var cc = utils.CallerWithDepth(xerror_conf.Conf.CallDepth + i)
if cc == "" {
break
}
err1.Caller = append(err1.Caller, cc)
}
var msg = err1.debugString()
if len(debugs) > 0 {
p(msg)
return
}
t.Fatal(msg)
}