-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.go
executable file
·259 lines (228 loc) · 5.43 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package errs
import (
"errors"
"fmt"
"io"
"iter"
"strings"
)
// Separator is the default separator between elements of a single error
var Separator = ": "
type Error struct {
// underlying error
cause *Error
// Op operation where error occured
Op string `json:"op"`
// Msg is the user-friendly message returned to the client.
Msg []string `json:"message"`
// Details is the internal error message returned to the developer.
Details []any `json:"-"`
// Code is the error code of the error. When marshaled to JSON, it will be a string.
Code Code `json:"code"`
// show is a flag that indicates whether the error would be visible when wrapped by another error
show bool
// depth of the error tree
depth int
shownDepth int
}
// knownCode returns the first known code of the error and all underlying errors
func (e *Error) knownCode() Code {
for _, er := range all(e) {
if er.Code != Unknown {
return er.Code
}
}
return Unknown
}
// Error returns the error in the format "code: message\ninner_code: inner_message" for this error and SHOWN underlying errors.
func (e *Error) Error() string {
if e == nil {
return ""
}
buf := strings.Builder{}
e.writeTo(&buf)
if e.cause != nil {
// print the underlying error only if shown
for inner := range shown(e.cause) {
buf.WriteString("\n")
inner.writeTo(&buf)
}
}
return buf.String()
}
// String returns the error in the format "code: message".
func (e *Error) String() string {
var buf strings.Builder
e.writeTo(&buf)
return buf.String()
}
func (e *Error) writeTo(w io.StringWriter) {
w.WriteString(e.Code.String())
if len(e.Op) > 0 {
w.WriteString(Separator + e.Op)
}
msgs := strings.Join(cleanStrings(e.Msg), Separator)
if len(msgs) > 0 {
w.WriteString(Separator + msgs)
}
}
// Stack returns a description of the error and all it's underlying errors.
func (e *Error) Stack() string {
var buf strings.Builder
for i, er := range all(e) {
tabOffset := strings.Repeat("\t", i)
write := func(s string) {
buf.WriteString(tabOffset)
buf.WriteString(s)
}
write(er.String() + "\n")
for dx, d := range er.Details {
write(fmt.Sprintf("\t%d: %v\n", dx, d))
}
buf.WriteString("\n")
}
return buf.String()
}
// Unwrap returns the underlying error.
func (e *Error) Unwrap() error {
if e.cause == nil {
return nil
}
return e.cause
}
func (e *Error) wrap(inner *Error) {
e.cause = inner
if inner == nil {
return
}
e.depth = inner.depth + 1
e.shownDepth = inner.shownDepth
if e.show {
e.shownDepth++
}
e.Code = e.knownCode()
}
func (e *Error) Is(target error) bool {
var t *Error
if errors.As(target, &t) {
return equalNodes(e, t)
}
return false
}
// equalNodes was created because we can't even trust go to compare equality of the error structs.
// Comparison does not involve the underlying errors because we don't want to compare the entire error tree.
//
// The fields considered for equality are error codes and messages. It makes sense to leave details out because two errors
// might be the same but with different details.
func equalNodes(a, b *Error) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if a.Code != b.Code {
return false
}
if a.Op != b.Op {
return false
}
if len(a.Msg) != len(b.Msg) {
return false
}
for i := range a.Msg {
if a.Msg[i] != b.Msg[i] {
return false
}
}
return true
}
// WrapMsg wraps an underlying error with a new error, adding message to the error's previously existing message
func WrapMsg(err error, message ...string) error {
code := Unknown
er := convert(err)
if er != nil {
code = er.Code
}
return WrapCode(er, code, message...)
}
// Wrap wraps an underlying error `child` with a new error `parent`.
//
// - when the child error is nil, the parent error is returned as is.
//
// - when the parent error is nil, the child error is returned as is.
//
// - when both errors are nil, nil is returned.
func Wrap(child, parent error) error {
p := convert(parent)
c := convert(child)
switch {
case p == nil && c == nil:
return nil
case p == nil:
return c
case c == nil:
return p
default:
p.wrap(c)
return p
}
}
// Convert converts any error to an *Error type. If the error is already an *Error, it is returned as is.
// nil errors are returned as nil.
func Convert(err error) error {
if err == nil {
return nil
}
var e *Error
if errors.As(err, &e) {
return e
}
return &Error{
Code: Unknown,
Msg: []string{err.Error()},
}
}
// WrapCode wraps an underlying error with a new error, adding message to the error's previously existing message and setting the error code to code.
func WrapCode(err error, code Code, messages ...string) error {
er := convert(err)
if er == nil {
return B().Code(code).Msg(cleanStrings(messages)...).Err()
}
e := &Error{
Code: code,
Msg: cleanStrings(messages),
}
e.wrap(er)
return e
}
func convert(err error) *Error {
e := Convert(err)
if e == nil {
return nil
}
return e.(*Error)
}
// shown iterates and yields only errors that should be shown
func shown(e *Error) iter.Seq[*Error] {
return func(yield func(er *Error) bool) {
for er := e; er != nil && er.shownDepth > 0; er = er.cause {
if !er.show {
continue
}
if !yield(er) {
return
}
}
}
}
// all iterates all inner errors
func all(e *Error) iter.Seq2[int, *Error] {
return func(yield func(i int, er *Error) bool) {
for i, er := 0, e; er != nil; i, er = i+1, er.cause {
if !yield(i, er) {
return
}
}
}
}