-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherror.go
111 lines (95 loc) · 1.84 KB
/
error.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
package play
import (
"runtime"
"strconv"
"strings"
"time"
)
type Err struct {
id string
tip string
err error
time time.Time
attach map[string]interface{}
track []string
code int
}
func (e Err) Err() error {
return e.err
}
func (e Err) Attach() map[string]interface{} {
return e.attach
}
func (e Err) Tip() string {
return e.tip
}
func (e Err) Error() string {
if e.err != nil {
return e.err.Error()
}
return e.tip
}
func (e Err) Track() []string {
return e.track
}
func (e Err) Time() time.Time {
return e.time
}
func (e Err) Code() int {
return e.code
}
func (e Err) Previous() (err Err) {
if err, ok := e.err.(Err); ok {
return err
}
return
}
func (e Err) WrapTip(tip string) Err {
e.tip = tip
return e
}
func (e Err) WrapCode(code int) Err {
e.code = code
return e
}
func WrapErr(err error, kv ...interface{}) (e Err) {
if e, ok := err.(Err); ok {
len := len(kv) - 1
for i := 0; i < len; i += 2 {
if v, ok := kv[i].(string); ok {
e.attach[v] = kv[i+1]
}
}
return e
}
return _wrapErr(err, 0, "", kv)
}
func _wrapErr(e error, code int, tip string, kv []interface{}) (err Err) {
err.err = e
err.time = time.Now()
err.attach = make(map[string]interface{})
err.id = Generate28Id("", "")
err.code = code
err.tip = tip
len := len(kv) - 1
for i := 0; i < len; i += 2 {
if v, ok := kv[i].(string); ok {
err.attach[v] = kv[i+1]
}
}
for i := 2; i < 10; i++ {
if funcptr, file, line, ok := runtime.Caller(i); ok {
if i > 1 && strings.HasSuffix(file, "action.go") {
break
}
funcName := runtime.FuncForPC(funcptr).Name()
err.track = append(err.track, strings.Replace(file, BuildBasePath, "", 1)+":"+strconv.Itoa(line)+"->"+funcName[strings.Index(funcName, ".")+1:]+"()")
if strings.HasPrefix(funcName, "main.main") {
break
}
continue
}
break
}
return
}