-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
158 lines (135 loc) · 2.66 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package exterror
import (
"fmt"
"bytes"
"runtime"
"path"
"strings"
)
type Error map[string]interface{}
func Warp(err interface{}) Error {
this := Error{}
switch vType := err.(type) {
case string:
{
this["zerror"] = fmt.Errorf(vType)
}
case Error:
{
for key, val := range vType {
this[key] = val
}
}
case *Error:
{
if nil != vType {
for key, val := range *vType {
this[key] = val
}
}
}
case error:
{
this["zerror"] = vType
}
default:
{
panic(fmt.Errorf("Not Support Error Type:%#v", err))
}
}
return this
}
func New(err interface{}) Error {
this := Warp(err)
if _, ok := this["zcallstack"]; !ok {
this["zcallstack"] = WithCs()
}
return this
}
func Newf(f string, v ...interface{}) Error {
this := Warp(fmt.Errorf(f, v...))
if _, ok := this["zcallstack"]; !ok {
this["zcallstack"] = WithCs()
}
return this
}
func (this Error) WithField(key string, val interface{}) Error {
this[key] = val
return this
}
func (this Error) WithFields(nFields Error) Error {
for k, v := range nFields {
this[k] = v
}
return this
}
func (this Error) WithErr(err interface{}) Error {
for key, val := range Warp(err) {
this[key] = val
}
if _, ok := this["zcallstack"]; !ok {
this["zcallstack"] = WithCs()
}
return this
}
func (this Error) WithCs() Error {
if _, ok := this["zcallstack"]; !ok {
this["zcallstack"] = WithCs()
}
return this
}
func (this Error) Addr() *Error {
return &this
}
func (this Error) Cause() error {
if err, ok := this["zerror"]; ok {
switch vType := err.(type) {
case error:
{
return vType
}
}
}
return nil
}
func (this Error) Error() string {
return this.String()
}
func (this Error) String() string {
// TODO 使用 string.buffer
var buffer bytes.Buffer
buffer.Write([]byte(" "))
for key, item := range this {
fmt.Fprintf(&buffer, " %s=%s", key, item)
}
buffer.Write([]byte(" "))
return string(buffer.Bytes())
}
func (this Error) ToMaps() map[string]interface{} {
return this
}
func ToMaps(err interface{}) map[string]interface{} {
val := Warp(err)
return val
}
func WithCs() string {
pc := make([]uintptr, 7)
max := runtime.Callers(3, pc) // 参数2表示忽略 Callers() WithCs() 这两层调用
callStacks := []string{}
for index, pcItem := range pc {
if index >= max {
break
}
f := runtime.FuncForPC(pcItem)
file, line := f.FileLine(pcItem)
fullname := f.Name()
funcname := ""
index := strings.LastIndexAny(fullname, ".")
if index >= 0 {
funcname = fullname[index+1:]
}
callfunc := fmt.Sprintf("%s@%s:%d", funcname, path.Base(file), line)
callStacks = append(callStacks, callfunc)
}
return strings.Join(callStacks, " ")
}