-
Notifications
You must be signed in to change notification settings - Fork 0
/
err.go
182 lines (162 loc) · 3.34 KB
/
err.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
package log
import (
"bytes"
"errors"
"fmt"
"path"
"runtime"
"strconv"
"strings"
)
func Define(info string) *traceError {
return &traceError{
error: errors.New(info),
}
}
func NewError(info ...interface{}) *traceError {
return TraceEx(1, errors.New(sprint(info)))
}
func NewErrorf(format string, info ...interface{}) *traceError {
return TraceEx(1, errors.New(sprintf(format, info)))
}
func EqualAny(e error, es []error) bool {
for i := 0; i < len(es); i++ {
if Equal(e, es[i]) {
return true
}
}
return false
}
func Equal(e1, e2 error) bool {
if e, ok := e1.(*traceError); ok {
e1 = e.error
}
if e, ok := e2.(*traceError); ok {
e2 = e.error
}
return e1 == e2
}
type traceError struct {
error
format []interface{}
stack []string
code *int
}
func (t *traceError) SetCode(code int) *traceError {
if t.stack == nil {
t = TraceEx(1, t)
}
t.code = &code
return t
}
func (t *traceError) GetCode() int {
if t.code == nil {
return 500
}
return *t.code
}
func (t *traceError) Error() string {
if t == nil {
return "<nil>"
}
if t.format == nil {
if t.error == nil {
panic(t.stack)
}
return t.error.Error()
}
return fmt.Sprintf(t.error.Error(), t.format...)
}
func (t *traceError) Trace(info ...interface{}) *traceError {
return TraceEx(1, t, info...)
}
func (t *traceError) Follow(err error) *traceError {
if t == nil {
return nil
}
if te, ok := err.(*traceError); ok {
if len(te.stack) > 0 {
te.stack[len(te.stack)-1] += ":" + err.Error()
}
t.stack = append(te.stack, t.stack...)
}
return t
}
func (t *traceError) Format(obj ...interface{}) *traceError {
if t.stack == nil {
t = TraceEx(1, t)
}
t.format = obj
return t
}
func (t *traceError) StackError() string {
if t == nil {
return t.Error()
}
if len(t.stack) == 0 {
return t.Error()
}
return fmt.Sprintf("[%s] %s", strings.Join(t.stack, ";"), t.Error())
}
func Tracefmt(layout string, objs ...interface{}) error {
var teInfo *traceError
for idx, obj := range objs {
if te, ok := obj.(*traceError); ok {
teInfo = te
objs[idx] = te.Error()
}
}
return &traceError{
error: fmt.Errorf(layout, objs...),
format: teInfo.format,
stack: teInfo.stack,
code: teInfo.code,
}
}
func Tracef(err error, obj ...interface{}) *traceError {
e := TraceEx(1, err).Format(obj...)
return e
}
// set runtime info to error
func TraceError(err error, info ...interface{}) *traceError {
return TraceEx(1, err, info...)
}
func Trace(err error, info ...interface{}) error {
if err == nil {
return nil
}
return TraceEx(1, err, info...)
}
func joinInterface(info []interface{}, ch string) string {
ret := bytes.NewBuffer(make([]byte, 0, 512))
for idx, o := range info {
if idx > 0 {
ret.WriteString(ch)
}
ret.WriteString(fmt.Sprint(o))
}
return ret.String()
}
func TraceEx(depth int, err error, info ...interface{}) *traceError {
if err == nil {
return nil
}
pc, _, line, _ := runtime.Caller(1 + depth)
name := runtime.FuncForPC(pc).Name()
name = path.Base(name)
stack := name + ":" + strconv.Itoa(line)
if len(info) > 0 {
stack += "(" + joinInterface(info, ",") + ")"
}
if te, ok := err.(*traceError); ok {
if te.stack == nil { // define
return &traceError{
error: te.error,
stack: []string{stack},
}
}
te.stack = append(te.stack, stack)
return te
}
return &traceError{err, nil, []string{stack}, nil}
}