-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_node.go
367 lines (330 loc) · 8.75 KB
/
error_node.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package tower
import (
"bytes"
"context"
"encoding/json"
"strconv"
"strings"
"time"
)
const codeBlockIndent = " "
// ErrorNode is the implementation of the Error interface.
type ErrorNode struct {
inner *errorBuilder
prev *ErrorNode
next *ErrorNode
}
// sorted keys are rather important for human reads. Especially the Context and Error should always be at the last marshaled keys.
// as they contain the most amount of data and information, and thus shadows other values at a glance.
//
// arguably this is simpler to be done than implementing json.Marshaler interface and doing it manually, key by key
// without resorting to other libraries.
type implJsonMarshaler struct {
Time string `json:"time,omitempty"`
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Caller Caller `json:"caller,omitempty"`
Key string `json:"key,omitempty"`
Level string `json:"level,omitempty"`
Service *Service `json:"service,omitempty"`
Context any `json:"context,omitempty"`
Error error `json:"error,omitempty"`
}
func newImplJSONMarshaler(e Error, next error, ctx any, service *Service) implJsonMarshaler {
return implJsonMarshaler{
Time: e.Time().Format(time.RFC3339),
Code: e.Code(),
Message: e.Message(),
Caller: e.Caller(),
Key: e.Key(),
Level: e.Level().String(),
Context: ctx,
Error: richJsonError{next},
Service: service,
}
}
type marshalFlag uint8
func (m marshalFlag) Has(f marshalFlag) bool {
return m&f == f
}
func (m *marshalFlag) Set(f marshalFlag) {
*m |= f
}
func (m *marshalFlag) Unset(f marshalFlag) {
*m &= ^f
}
const (
marshalSkipCode marshalFlag = 1 << iota
marshalSkipMessage
marshalSkipLevel
marshalSkipCaller
marshalSkipContext
marshalSkipTime
marshalSkipService
marshalSkipAll = marshalSkipCode +
marshalSkipMessage +
marshalSkipLevel +
marshalSkipTime +
marshalSkipContext +
marshalSkipCaller +
marshalSkipService
)
func (e *ErrorNode) createMarshalJSONFlag() marshalFlag {
var m marshalFlag
// The logic below for condition flow:
//
// if the next error is not an ErrorNode, denoted by e.next == nil, we will test against the error implements
// Error interface, and deduplicate the fields in this current node. But only when the previous node is also an
// ErrorNode.
//
// Unlike in CodeBlock for human read first, where the innermost error is the most important.
//
// the Logic for MarshalJSON is aimed towards machine and log parsers.
//
// The outermost error is the most important fields for indexing, and thus we will not skip any fields.
//
// However, any nested error with duplicate values will be just a waste of space and bandwidth, so we will skip them.
if e.prev == nil {
return m
}
other, ok := e.inner.origin.(Error)
if e.prev == nil && e.next == nil && !ok {
return m
}
prev, current := e.prev, e
if prev.Code() == current.Code() {
m.Set(marshalSkipCode)
}
if prev.Level() == current.Level() {
m.Set(marshalSkipLevel)
}
// e.next != nil because we don't want message skipped when it's the last error in the chain.
if prev.Message() == current.Message() && e.next != nil {
m.Set(marshalSkipMessage)
}
if len(current.Context()) == 0 {
m.Set(marshalSkipContext)
}
if prev.Time().Sub(current.Time()) < time.Second {
m.Set(marshalSkipTime)
}
if prev.inner.tower.service == current.inner.tower.service {
m.Set(marshalSkipService)
}
if ok {
m |= e.deduplicateAgainstOtherError(other)
}
if m.Has(marshalSkipCode) &&
m.Has(marshalSkipMessage) &&
m.Has(marshalSkipLevel) &&
m.Has(marshalSkipContext) &&
m.Has(marshalSkipTime) &&
m.Has(marshalSkipService) {
m.Set(marshalSkipCaller)
}
return m
}
func (e *ErrorNode) createPayload(m marshalFlag) *implJsonMarshaler {
ctx := func() any {
if len(e.inner.context) == 0 {
return nil
}
if len(e.inner.context) == 1 {
return e.inner.context[0]
}
return e.inner.context
}()
var next error
if e.next != nil {
next = e.next
} else {
next = e.inner.origin
}
marshalAble := newImplJSONMarshaler(e, next, ctx, &e.inner.tower.service)
if m.Has(marshalSkipCode) {
marshalAble.Code = 0
}
if m.Has(marshalSkipMessage) {
marshalAble.Message = ""
}
if m.Has(marshalSkipLevel) {
marshalAble.Level = ""
}
if m.Has(marshalSkipTime) {
marshalAble.Time = ""
}
if m.Has(marshalSkipCaller) {
marshalAble.Caller = nil
}
if m.Has(marshalSkipService) {
marshalAble.Service = nil
}
return &marshalAble
}
func (e *ErrorNode) MarshalJSON() ([]byte, error) {
if e == nil {
return []byte("null"), nil
}
m := e.createMarshalJSONFlag()
b := &bytes.Buffer{}
enc := json.NewEncoder(b)
enc.SetEscapeHTML(false)
if m.Has(marshalSkipAll) {
err := enc.Encode(richJsonError{e.inner.origin})
return b.Bytes(), err
}
err := enc.Encode(e.createPayload(m))
return b.Bytes(), err
}
func (e *ErrorNode) Error() string {
s := &strings.Builder{}
lw := NewLineWriter(s).LineBreak(": ").Build()
e.WriteError(lw)
return s.String()
}
// WriteError Writes the error.Error to the writer instead of being allocated as value.
func (e *ErrorNode) WriteError(w LineWriter) {
w.WriteIndent()
msg := e.inner.message
if e.inner.origin == nil {
// Account for empty string message after wrapping nil error.
if len(msg) > 0 {
w.WritePrefix()
_, _ = w.WriteString(msg)
w.WriteSuffix()
w.WriteLineBreak()
}
w.WritePrefix()
_, _ = w.WriteString("[nil]")
w.WriteSuffix()
return
}
writeInner := func(linebreak bool) {
if ew, ok := e.inner.origin.(ErrorWriter); ok {
if linebreak {
w.WriteLineBreak()
}
ew.WriteError(w)
} else {
errMsg := e.inner.origin.Error()
if errMsg != msg {
w.WriteLineBreak()
w.WritePrefix()
_, _ = w.WriteString(errMsg)
w.WriteSuffix()
}
}
}
var innerMessage string
if mh, ok := e.inner.origin.(MessageHint); ok {
innerMessage = mh.Message()
}
// Skip writing duplicate or empty messages.
if msg == innerMessage || len(msg) == 0 {
writeInner(false)
return
}
w.WritePrefix()
_, _ = w.WriteString(msg)
w.WriteSuffix()
writeInner(true)
}
// Code Gets the original code of the type.
func (e *ErrorNode) Code() int {
return e.inner.code
}
// HTTPCode Gets HTTP Status Code for the type.
func (e *ErrorNode) HTTPCode() int {
switch {
case e.inner.code >= 200 && e.inner.code <= 599:
return e.inner.code
case e.inner.code > 999:
code := e.inner.code % 1000
if code >= 200 && code <= 599 {
return code
}
}
return 500
}
// Message Gets the Message of the type.
func (e *ErrorNode) Message() string {
return e.inner.message
}
// Caller Gets the caller of this type.
func (e *ErrorNode) Caller() Caller {
return e.inner.caller
}
// Context Gets the context of this type.
func (e *ErrorNode) Context() []any {
return e.inner.context
}
func (e *ErrorNode) Level() Level {
return e.inner.level
}
func (e *ErrorNode) Time() time.Time {
return e.inner.time
}
func (e *ErrorNode) Key() string {
return e.inner.key
}
func (e *ErrorNode) Service() Service {
return e.inner.tower.service
}
// Unwrap Returns the error that is wrapped by this error. To be used by errors.Is and errors.As functions from errors library.
func (e *ErrorNode) Unwrap() error {
return e.inner.origin
}
// Log this error.
func (e *ErrorNode) Log(ctx context.Context) Error {
e.inner.tower.LogError(ctx, e)
return e
}
// Notify this error to Messengers.
func (e *ErrorNode) Notify(ctx context.Context, opts ...MessageOption) Error {
e.inner.tower.NotifyError(ctx, e, opts...)
return e
}
// richJsonError is a special kind of error that tries to prevent information loss when marshaling to json.
type richJsonError struct {
error
}
func (r richJsonError) MarshalJSON() ([]byte, error) {
if r.error == nil {
return []byte("null"), nil
}
// if the error supports json.Marshaler we use it directly.
// this is because we can assume that the error have special marshaling needs for specific output.
//
// E.G. to prevent unnecessary "summary" keys when the origin error is already is a tower.Error type.
if e, ok := r.error.(json.Marshaler); ok { //nolint
return e.MarshalJSON()
}
b := &bytes.Buffer{}
enc := json.NewEncoder(b)
enc.SetEscapeHTML(false)
err := enc.Encode(r.error)
if err != nil {
_ = enc.Encode(r.error.Error())
return b.Bytes(), nil
}
summary := r.error.Error()
// 3 because it also includes newline after brackets or quotes.
//
// The logic below looks for: ""\n, {}\n, []\n
if b.Len() == 3 && b.Bytes()[2] == '\n' {
v := b.Bytes()
switch {
case v[0] == '"', v[0] == '{', v[0] == '[':
b.Reset()
err := enc.Encode(map[string]string{"summary": summary})
return b.Bytes(), err
}
}
content := b.String()
b.Reset()
err = enc.Encode(map[string]json.RawMessage{
"details": json.RawMessage(content),
"summary": json.RawMessage(strconv.Quote(summary)),
})
return b.Bytes(), err
}