-
Notifications
You must be signed in to change notification settings - Fork 1
/
fields.go
241 lines (213 loc) · 5.32 KB
/
fields.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
package errors
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/mailgun/errors/callstack"
)
// HasFields Implement this interface to pass along unstructured context to the logger.
// It is the responsibility of Fields() implementation to unwrap the error chain and
// collect all errors that have `HasFields()` defined.
type HasFields interface {
HasFields() map[string]any
}
// HasFormat True if the interface has the format method (from fmt package)
type HasFormat interface {
Format(st fmt.State, verb rune)
}
// Fields Creates errors that conform to the `HasFields` interface
type Fields map[string]any
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is call, and the format specifier.
// If err is nil, Wrapf returns nil.
func (f Fields) Wrapf(err error, format string, args ...any) error {
if err == nil {
return nil
}
return &fields{
stack: callstack.New(1),
fields: f,
wrapped: err,
msg: fmt.Sprintf(format, args...),
}
}
// WrapFields returns a new error wrapping the provided error with fields and a message.
func WrapFields(err error, f Fields, msg string) error {
if err == nil {
return nil
}
return &fields{
stack: callstack.New(1),
wrapped: err,
msg: msg,
fields: f,
}
}
// WrapFieldsf is identical to WrapFields but with optional formatting
func WrapFieldsf(err error, f Fields, format string, args ...any) error {
if err == nil {
return nil
}
return &fields{
msg: fmt.Sprintf(format, args...),
stack: callstack.New(1),
wrapped: err,
fields: f,
}
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func (f Fields) Wrap(err error, msg string) error {
if err == nil {
return nil
}
return &fields{
stack: callstack.New(1),
fields: f,
wrapped: err,
msg: msg,
}
}
// Stack returns an error annotating err with a stack trace
// at the point Stack is called. If err is nil, Stack returns nil.
func (f Fields) Stack(err error) error {
if err == nil {
return nil
}
return &fields{
stack: callstack.New(1),
fields: f,
wrapped: err,
}
}
func (f Fields) Error(msg string) error {
return &fields{
stack: callstack.New(1),
fields: f,
wrapped: errors.New(msg),
msg: "",
}
}
func (f Fields) Errorf(format string, args ...any) error {
return &fields{
stack: callstack.New(1),
fields: f,
wrapped: fmt.Errorf(format, args...),
msg: "",
}
}
type fields struct {
fields Fields
msg string
wrapped error
stack *callstack.CallStack
}
func (c *fields) Unwrap() error {
return c.wrapped
}
func (c *fields) Is(target error) bool {
_, ok := target.(*fields)
return ok
}
// Cause returns the wrapped error which was the original
// cause of the issue. We only support this because some code
// depends on github.com/pkg/errors.Cause() returning the cause
// of the error.
// Deprecated: use error.Is() or error.As() instead
func (c *fields) Cause() error { return c.wrapped }
func (c *fields) Error() string {
if c.msg == NoMsg {
return c.wrapped.Error()
}
return c.msg + ": " + c.wrapped.Error()
}
func (c *fields) StackTrace() callstack.StackTrace {
if child, ok := c.wrapped.(callstack.HasStackTrace); ok {
return child.StackTrace()
}
return c.stack.StackTrace()
}
func (c *fields) HasFields() map[string]any {
result := make(map[string]any, len(c.fields))
for key, value := range c.fields {
result[key] = value
}
// child fields have precedence as they are closer to the cause
var f HasFields
if errors.As(c.wrapped, &f) {
child := f.HasFields()
if child == nil {
return result
}
for key, value := range child {
result[key] = value
}
}
return result
}
func (c *fields) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
if c.msg == NoMsg {
_, _ = fmt.Fprintf(s, "%+v (%s)", c.Unwrap(), c.FormatFields())
return
}
_, _ = fmt.Fprintf(s, "%s: %+v (%s)", c.msg, c.Unwrap(), c.FormatFields())
return
}
fallthrough
case 's', 'q':
_, _ = io.WriteString(s, c.Error())
return
}
}
func (c *fields) FormatFields() string {
var buf bytes.Buffer
var count int
for key, value := range c.fields {
if count > 0 {
buf.WriteString(", ")
}
buf.WriteString(fmt.Sprintf("%+v=%+v", key, value))
count++
}
return buf.String()
}
// ToMap Returns the fields for the underlying error as map[string]any
// If no fields are available returns nil
func ToMap(err error) map[string]any {
if err == nil {
return nil
}
result := map[string]any{
"excValue": err.Error(),
"excType": fmt.Sprintf("%T", Unwrap(err)),
}
// Find any errors with StackTrace information if available
var stack callstack.HasStackTrace
if Last(err, &stack) {
trace := stack.StackTrace()
caller := callstack.GetLastFrame(trace)
result["excFuncName"] = caller.Func
result["excLineNum"] = caller.LineNo
result["excFileName"] = caller.File
}
// Search the error chain for fields
var f HasFields
if errors.As(err, &f) {
for key, value := range f.HasFields() {
result[key] = value
}
}
return result
}
// ToLogrus Returns the context and stacktrace information for the underlying error
// that could be used as logrus.Fields
//
// logrus.Fields(errors.ToLogrus(err)).WithField("tid", 1).Error(err)
func ToLogrus(err error) map[string]any {
return ToMap(err)
}