-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
241 lines (214 loc) · 4.49 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package zerror
import (
"context"
"errors"
"fmt"
"runtime"
"strconv"
"strings"
"sync"
)
type Def struct {
// error code,
Code string `json:"code"`
Msg string `json:"msg"`
Description string `json:"desc"`
// can be used as http status code or grpc status code
Status Status `json:"status"`
// extended fields
extensions map[string]interface{}
}
type Data map[string]interface{}
type ZContext struct {
callerLoc string
callerName string
Data Data
Ctx context.Context
}
func (ctx *ZContext) Merge(m *ZContext) {
ctx.callerLoc = m.callerLoc
ctx.callerName += `/` + m.callerName
for k, v := range m.Data {
ctx.Data[k] = v
}
}
type Error struct {
cause error
*Def
msg string
ZContext
}
func (ze *Error) Unwrap() error {
return ze.cause
}
func (ze *Error) Error() string {
b := strings.Builder{}
b.WriteString(ze.Def.Code)
if ze.msg != `` {
b.WriteString(`(`)
b.WriteString(ze.msg)
b.WriteString(`)`)
}
if ze.cause != nil {
b.WriteString(` | `)
b.WriteString(ze.cause.Error())
}
return b.String()
}
func (ze *Error) WithData(data Data) *Error {
if ze.Data == nil {
ze.Data = make(Data, len(data)+2)
}
for k, v := range data {
ze.Data[k] = v
}
return ze
}
func (ze *Error) WithKVs(kvs ...interface{}) *Error {
if ze.Data == nil {
ze.Data = make(Data, len(kvs))
}
for i := 0; i < len(kvs); i += 2 {
k, ok := kvs[i].(string)
if !ok {
k = fmt.Sprintf(`%v`, kvs[i])
}
var v interface{}
if i+1 < len(kvs) {
v = kvs[i+1]
}
ze.Data[k] = v
}
return ze
}
func (ze *Error) WithCtx(ctx context.Context) *Error {
ze.Ctx = ctx
return ze
}
func (ze *Error) GetCaller() (string, string) {
return ze.callerLoc, ze.callerName
}
func (ze *Error) Render() Render {
def := ze.Def
s := renderPool.Get().(Render)
code := def.Code
if code != Internal.Code && Internal.Cause(ze.cause) {
code = Internal.Code
}
s.SetCode(code)
if Manager.respondMsgSet && Manager.respondMessage ||
!Manager.respondMsgSet && Manager.debugMode {
s.SetMessage(ze.Error())
}
return s
}
func (def *Def) Extend(k string, v interface{}) *Def {
if def.extensions == nil {
def.extensions = make(map[string]interface{})
}
def.extensions[k] = v
return def
}
func (def *Def) GetExtension(key string) (interface{}, bool) {
if def.extensions == nil {
return nil, false
}
value, ok := def.extensions[key]
return value, ok
}
func (def *Def) wrapf(err error, skip int, format string, args ...interface{}) *Error {
l, n := GetCaller(def, skip)
zCause := &Error{}
zErr := &Error{
cause: err,
Def: def,
}
if ok := errors.As(err, &zCause); ok {
zErr.ZContext = zCause.ZContext
zErr.callerName += `/` + n
} else {
zErr.ZContext = ZContext{
callerLoc: l,
callerName: n,
Data: make(Data),
}
}
if format != `` {
zErr.msg = fmt.Sprintf(format, args...)
}
return zErr
}
func (def *Def) Wrap(err error) *Error {
return def.wrapf(err, 3, ``)
}
func (def *Def) Wrapf(err error, format string, args ...interface{}) *Error {
return def.wrapf(err, 3, format, args...)
}
func (def *Def) WithMsg(msg string) *Error {
return def.wrapf(nil, 3, msg)
}
func (def *Def) New() *Error {
return def.wrapf(nil, 3, ``)
}
func (def *Def) Errorf(format string, args ...interface{}) *Error {
err := errors.New(fmt.Sprintf(format, args...))
return def.wrapf(err, 3, ``)
}
var renderPool = sync.Pool{
New: func() interface{} {
render := Manager.render()
reset, ok := render.(Resetter)
if ok {
reset.Reset()
}
return render
},
}
func (def *Def) Cause(err error) bool {
zerr := &Error{}
for {
if ok := errors.As(err, &zerr); ok {
if zerr.Def == def {
return true
} else {
err = zerr.cause
}
} else {
return false
}
}
}
func FromCode(code string) (*Error, bool) {
def, ok := defMap[code]
if !ok {
return nil, ok
}
return def.New(), true
}
func GetCaller(def *Def, skip int) (loc string, name string) {
pc, file, line, ok := runtime.Caller(skip)
if ok && (Manager.debugMode || def == nil || def.Code == CodeInternal) {
loc = file + "/" + strconv.Itoa(line)
}
if ok {
funcName := runtime.FuncForPC(pc).Name()
name = funcName[strings.LastIndexByte(funcName, '.')+1:]
}
return
}
// add '-' before initial capital letters and turn lower
func getStandardName(name string) string {
out := ``
lastLower := true
for k, v := range name {
if v >= 'A' && v <= 'Z' && k != 0 && lastLower {
out += `-`
lastLower = false
} else {
lastLower = true
}
out += string(v)
}
lowered := strings.ToLower(out)
return lowered
}