-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
241 lines (211 loc) · 4.2 KB
/
errors.go
File metadata and controls
241 lines (211 loc) · 4.2 KB
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 errors
import (
"context"
"errors"
"fmt"
"io"
"strings"
)
var (
New = errors.New
As = errors.As
Is = errors.Is
Unwrap = errors.Unwrap
Join = errors.Join
)
type Modifier interface {
SetCode(code StatusCode)
WrapMessage(msg string)
AppendDetails(details ...Any)
}
type Annotation interface {
Annotate(m Modifier)
}
type annotated struct {
cause error
code StatusCode
message string
details []Any
}
func (e *annotated) SetCode(code StatusCode) {
e.code = code
}
func (e *annotated) AppendDetails(details ...Any) {
e.details = append(e.details, details...)
}
func (e *annotated) WrapMessage(msg string) {
next := msg + ": " + e.message
e.message = strings.TrimPrefix(next, ": ")
}
func (e annotated) Unwrap() error { return e.cause }
func (e annotated) Error() string { return e.message }
func (e annotated) Format(f fmt.State, verb rune) {
switch verb {
case 'v':
if f.Flag('+') {
_, _ = fmt.Fprintf(f, "status: %q\n", e.code.Error())
_, _ = fmt.Fprintf(f, "message: %q\n", e.Error())
for i, detail := range e.details {
_, _ = fmt.Fprintf(f, "detail[%d]:\n", i)
str := fmt.Sprintf("\t%+v", detail)
str = strings.ReplaceAll(str, "\n", "\n\t")
_, _ = io.WriteString(f, str[0:len(str)-1])
}
return
}
fallthrough
case 's':
_, _ = io.WriteString(f, e.Error())
case 'q':
_, _ = fmt.Fprintf(f, "%q", e.Error())
}
}
func Annotate(cause error, annotations ...Annotation) error {
if cause == nil || len(annotations) == 0 {
return cause
}
err := &annotated{
cause: cause,
message: cause.Error(),
}
if code, ok := cause.(StatusCode); ok {
err.code = code
}
for _, annotation := range annotations {
annotation.Annotate(err)
}
return err
}
type Message string
func (a Message) Annotate(m Modifier) {
m.WrapMessage(string(a))
}
func Code(err error) (out StatusCode) {
travel(err, visitor{
OnCode: func(code StatusCode) bool {
out = code
return out == OK
},
})
if err == nil || out != OK {
return
}
switch {
case errors.Is(err, context.DeadlineExceeded):
out = DeadlineExceeded
case errors.Is(err, context.Canceled):
out = Cancelled
default:
out = Unknown
}
return
}
func Temporary(err error) (out bool) {
travel(err, visitor{
OnError: func(err error) bool {
if tErr, ok := err.(interface {
Temporary() bool
}); ok {
out = tErr.Temporary()
return false
}
return true
},
})
return
}
func Details(err error) (out []Any) {
travel(err, visitor{
OnDetails: func(details []Any) bool {
out = append(out, details...)
return true
},
})
return
}
func HideDebugInfo(a Any) Any {
if a.TypeUrl() == TypeUrlDebugInfo {
return nil
}
return a
}
type DetailMapper func(a Any) Any
type detailMappers []DetailMapper
func (fn detailMappers) Map(detail Any) (out Any) {
out = detail
for _, mapper := range fn {
if out == nil {
return
}
out = mapper(out)
}
return
}
func Flatten(err error, mappers ...DetailMapper) error {
var o annotated
travel(err, visitor{
OnCode: func(code StatusCode) bool {
if o.code == OK {
o.code = code
}
return true
},
OnDetails: func(details []Any) bool {
mapper := detailMappers(mappers)
for _, detail := range details {
if d := mapper.Map(detail); d != nil {
o.details = append(o.details, d)
}
}
return true
},
OnError: func(cur error) bool {
if o.message == "" {
o.message = cur.Error()
}
o.cause = cur
return true
},
})
if o.code == OK {
switch o.cause {
case nil:
// Keep OK
case context.Canceled:
o.code = Cancelled
case context.DeadlineExceeded:
o.code = DeadlineExceeded
default:
o.code = Unknown
}
}
return &o
}
type visitor struct {
OnCode func(code StatusCode) bool
OnDetails func(details []Any) bool
OnError func(err error) bool
}
func travel(root error, v visitor) {
cur := root
Loop:
for cur != nil {
switch a := cur.(type) {
case StatusCode:
if v.OnCode != nil && !v.OnCode(a) {
break Loop
}
case *annotated:
if v.OnCode != nil && !v.OnCode(a.code) {
break Loop
}
if v.OnDetails != nil && !v.OnDetails(a.details) {
break Loop
}
}
if v.OnError != nil && !v.OnError(cur) {
break Loop
}
cur = errors.Unwrap(cur)
}
}