-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
placeholder.go
305 lines (263 loc) · 6.93 KB
/
placeholder.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package errors
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
)
type placeholderStackTracer interface {
StackTrace() placeholderStack
}
// UnmarshalJSON unnmarshals JSON errors into placeholder errors
// which can then be formatted in the same way as other errors
// from this package.
//
// Placeholder errors contain same data as original errors (those
// marshaled into JSON), but have multiple limitations:
//
// 1. They do not implement stackTracer interface because addresses
// of stack frames are not available in JSON nor they are portable.
// 2. Placeholder errors are not of the same type as original errors.
// Thus errors.Is and errors.As do not work.
// 3. The implementation of fmt.Formatter interface of the original error
// is not used when formatting placeholder errors.
//
// Placeholder errors also have different trees of wrapping errors than
// original errors because during JSON marshal potentially multiple levels
// of wrapping are combined into one JSON object. Nested objects happen
// only for errors implementing causer or unwrapper interface returning
// multiple errors.
func UnmarshalJSON(data []byte) (error, E) { //nolint:revive,stylecheck
if bytes.Equal(data, []byte("null")) {
return nil, nil //nolint:nilnil
}
var payload map[string]json.RawMessage
err := json.Unmarshal(data, &payload)
if err != nil {
return nil, WithStack(err)
}
var errE E
var msg string
var s placeholderStack
var errs []error
var cause error
errorData, ok := payload["error"]
delete(payload, "error")
if ok {
err := json.Unmarshal(errorData, &msg)
if err != nil {
return nil, WithMessage(err, "error")
}
}
stackData, ok := payload["stack"]
delete(payload, "stack")
if ok {
err := json.Unmarshal(stackData, &s)
if err != nil {
return nil, WithMessage(err, "stack")
}
if len(s) == 0 {
s = nil
}
}
causeData, ok := payload["cause"]
delete(payload, "cause")
if ok {
cause, errE = UnmarshalJSON(causeData)
if errE != nil {
return nil, WithMessage(errE, "cause")
}
}
errorsData, ok := payload["errors"]
delete(payload, "errors")
if ok {
var errorsSliceData []json.RawMessage
err := json.Unmarshal(errorsData, &errorsSliceData)
if err != nil {
return nil, WithMessage(err, "errors")
}
for i, d := range errorsSliceData {
e, errE := UnmarshalJSON(d)
if errE != nil {
return nil, WithMessagef(errE, "errors: %d", i)
}
if e != nil {
// If e is equal to cause, we want to have e be the same pointer to cause, so that
// handling of wrapError-like errors can be simplified in formatting and JSON marshal.
if cause != nil && reflect.DeepEqual(e, cause) { //nolint:govet
errs = append(errs, cause)
} else {
errs = append(errs, e)
}
}
}
if len(errs) == 0 {
errs = nil
}
}
details := map[string]interface{}{}
for key, value := range payload {
var v interface{}
err := json.Unmarshal(value, &v)
if err != nil {
return nil, WithMessage(err, key)
}
details[key] = v
}
if cause != nil && len(errs) > 0 {
return &placeholderJoinedCauseError{
msg: msg,
stack: s,
details: details,
cause: cause,
errs: errs,
}, nil
} else if cause != nil {
return &placeholderCauseError{
msg: msg,
stack: s,
details: details,
cause: cause,
}, nil
} else if len(errs) > 0 {
return &placeholderJoinedError{
msg: msg,
stack: s,
details: details,
errs: errs,
}, nil
}
return &placeholderError{
msg: msg,
stack: s,
details: details,
}, nil
}
type placeholderFrame struct {
Name string `json:"name,omitempty"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
}
type placeholderStack []placeholderFrame
func (s placeholderStack) Format(st fmt.State, verb rune) {
for _, f := range s {
frame{ //nolint:exhaustruct
Function: f.Name,
Line: f.Line,
File: f.File,
}.Format(st, verb)
_, _ = io.WriteString(st, "\n")
}
}
type placeholderError struct {
msg string
stack placeholderStack
details map[string]interface{}
}
func (e *placeholderError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *placeholderError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e placeholderError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *placeholderError) StackTrace() placeholderStack {
return e.stack
}
func (e *placeholderError) Details() map[string]interface{} {
return e.details
}
type placeholderCauseError struct {
msg string
stack placeholderStack
details map[string]interface{}
cause error
}
func (e *placeholderCauseError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *placeholderCauseError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e placeholderCauseError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *placeholderCauseError) StackTrace() placeholderStack {
return e.stack
}
func (e *placeholderCauseError) Details() map[string]interface{} {
return e.details
}
func (e *placeholderCauseError) Unwrap() error {
return e.cause
}
func (e *placeholderCauseError) Cause() error {
return e.cause
}
type placeholderJoinedError struct {
msg string
stack placeholderStack
details map[string]interface{}
errs []error
}
func (e *placeholderJoinedError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *placeholderJoinedError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e placeholderJoinedError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *placeholderJoinedError) StackTrace() placeholderStack {
return e.stack
}
func (e *placeholderJoinedError) Details() map[string]interface{} {
return e.details
}
func (e *placeholderJoinedError) Unwrap() []error {
return e.errs
}
type placeholderJoinedCauseError struct {
msg string
stack placeholderStack
details map[string]interface{}
cause error
errs []error
}
func (e *placeholderJoinedCauseError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *placeholderJoinedCauseError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e placeholderJoinedCauseError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *placeholderJoinedCauseError) StackTrace() placeholderStack {
return e.stack
}
func (e *placeholderJoinedCauseError) Details() map[string]interface{} {
return e.details
}
func (e *placeholderJoinedCauseError) Unwrap() []error {
return e.errs
}
func (e *placeholderJoinedCauseError) Cause() error {
return e.cause
}