-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.go
195 lines (160 loc) · 3.1 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
package util
import (
"fmt"
"io"
"runtime"
"strings"
"github.com/pkg/errors"
)
type NError struct {
*stack
id string
msg string
wrapped error
}
func NewError(s string, a ...interface{}) *NError {
var pcs [1]uintptr
_ = runtime.Callers(2, pcs[:])
f := errors.Frame(pcs[0])
return &NError{
id: fmt.Sprintf("%n:%d", f, f),
msg: strings.TrimSpace(fmt.Sprintf(s, a...)),
}
}
func (er *NError) Error() string {
i := er.msg
if er.wrapped != nil {
j := er.wrapped.Error()
if len(j) > 0 {
i += "; " + j
}
}
return i
}
func (er *NError) Unwrap() error {
return er.wrapped
}
func (er *NError) Is(err error) bool {
i, ok := err.(*NError) // nolint:errorlint
if !ok {
return false
}
if i.id == er.id {
return true
}
if er.wrapped == nil {
return false
}
return errors.Is(er.wrapped, err)
}
func (*NError) As(err interface{}) bool { // nolint:govet
_, ok := err.(*NError) // nolint:errorlint
return ok
}
func (er *NError) Wrap(err error) *NError {
return &NError{
id: er.id,
msg: er.msg,
stack: callers(3),
wrapped: err,
}
}
func (er *NError) Errorf(s string, a ...interface{}) *NError {
return &NError{
id: er.id,
msg: er.msg,
stack: callers(3),
wrapped: fmt.Errorf(s, a...),
}
}
func (er *NError) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
if st.Flag('+') {
ws := er.wrapped != nil || er.stack != nil
if ws {
_, _ = fmt.Fprintf(st, "%s", er.msg)
}
if er.stack != nil {
er.stack.Format(st, verb)
}
if er.wrapped != nil {
var d string
if len(er.msg) > 0 {
d = "; "
}
_, _ = fmt.Fprintf(st, "%s%+v", d, er.wrapped)
}
if ws {
return
}
}
fallthrough
case 's':
_, _ = io.WriteString(st, er.Error())
case 'q':
_, _ = fmt.Fprintf(st, "%q", er.Error())
}
}
func (er *NError) Merge(err error) *NError {
return &NError{
id: er.id,
msg: er.msg,
wrapped: err,
}
}
func (er *NError) Call() *NError {
return &NError{
id: er.id,
msg: er.msg,
stack: callers(3),
}
}
func (er *NError) Caller(n int) *NError {
return &NError{
id: er.id,
msg: er.msg,
stack: callers(n),
wrapped: er.wrapped,
}
}
func (er *NError) StackTrace() errors.StackTrace {
if er.stack != nil {
return er.stack.StackTrace()
}
if er.wrapped == nil {
return nil
}
i, ok := er.wrapped.(stackTracer) // nolint:errorlint
if !ok {
return nil
}
return i.StackTrace()
}
// callers is from
// https://github.com/pkg/errors/blob/856c240a51a2bf8fb8269ea7f3f9b046aadde36e/stack.go#L163
func callers(skip int) *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(skip, pcs[:])
var st stack = pcs[0:n]
return &st
}
type stack []uintptr
func (s *stack) Format(st fmt.State, verb rune) {
if verb == 'v' && st.Flag('+') {
for _, pc := range *s {
_, _ = fmt.Fprintf(st, "\n%+v", errors.Frame(pc))
}
}
}
func (s *stack) StackTrace() errors.StackTrace {
f := make([]errors.Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = errors.Frame((*s)[i])
}
return f
}
type stackTracer interface {
StackTrace() errors.StackTrace
}