-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
264 lines (224 loc) · 5.46 KB
/
errors.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
package goerr
import (
"errors"
"fmt"
"io"
"strings"
"log/slog"
"github.com/google/uuid"
)
// New creates a new error with message
func New(format string, args ...any) *Error {
err := newError()
err.msg = fmt.Sprintf(format, args...)
return err
}
func toWrapMessage(msgs []any) string {
var newMsgs []string
for _, m := range msgs {
newMsgs = append(newMsgs, fmt.Sprintf("%v", m))
}
return strings.Join(newMsgs, " ")
}
// Wrap creates a new Error and add message.
func Wrap(cause error, msg ...any) *Error {
err := newError()
err.msg = toWrapMessage(msg)
err.cause = cause
return err
}
// Wrapf creates a new Error and add message. The error message is formatted by fmt.Sprintf.
func Wrapf(cause error, format string, args ...any) *Error {
err := newError()
err.msg = fmt.Sprintf(format, args...)
err.cause = cause
return err
}
// Unwrap returns unwrapped goerr.Error from err by errors.As. If no goerr.Error, returns nil
// NOTE: Do not receive error interface. It causes typed-nil problem.
//
// var err error = goerr.New("error")
// if err != nil { // always true
func Unwrap(err error) *Error {
var e *Error
if errors.As(err, &e) {
return e
}
return nil
}
type values map[string]any
func (x values) clone() values {
newValues := make(values)
for key, value := range x {
newValues[key] = value
}
return newValues
}
// Error is error interface for deepalert to handle related variables
type Error struct {
msg string
id string
st *stack
cause error
values values
}
func newError() *Error {
return &Error{
st: callers(),
values: make(values),
id: uuid.New().String(),
}
}
func (x *Error) copy(dst *Error) {
dst.msg = x.msg
dst.id = x.id
dst.cause = x.cause
for k, v := range x.values {
dst.values[k] = v
}
// st (stacktrace) is not copied
}
// Printable returns printable object
func (x *Error) Printable() *printable {
e := &printable{
Message: x.msg,
ID: x.id,
StackTrace: x.Stacks(),
Cause: x.cause,
Values: make(map[string]any),
}
for k, v := range x.values {
e.Values[k] = v
}
return e
}
type printable struct {
Message string `json:"message"`
ID string `json:"id"`
StackTrace []*Stack `json:"stacktrace"`
Cause error `json:"cause"`
Values map[string]any `json:"values"`
}
// Error returns error message for error interface
func (x *Error) Error() string {
s := x.msg
cause := x.cause
if cause == nil {
return s
}
s = fmt.Sprintf("%s: %v", s, cause.Error())
return s
}
// Format returns:
// - %v, %s, %q: formatted message
// - %+v: formatted message with stack trace
func (x *Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
_, _ = io.WriteString(s, x.Error())
var c *Error
for c = x; c.Unwrap() != nil; {
cause, ok := c.Unwrap().(*Error)
if !ok {
break
}
c = cause
}
c.st.Format(s, verb)
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, x.Error())
case 'q':
fmt.Fprintf(s, "%q", x.Error())
}
}
// Unwrap returns *fundamental of github.com/pkg/errors
func (x *Error) Unwrap() error {
return x.cause
}
// With adds key and value related to the error event
func (x *Error) With(key string, value any) *Error {
x.values[key] = value
return x
}
// Unstack trims stack trace by 1. It can be used for internal helper or utility functions.
func (x *Error) Unstack() *Error {
x.st = unstack(x.st, 1)
return x
}
// UnstackN trims stack trace by n. It can be used for internal helper or utility functions.
func (x *Error) UnstackN(n int) *Error {
x.st = unstack(x.st, n)
return x
}
// Is returns true if target is goerr.Error and Error.id of two errors are matched. It's for errors.Is. If Error.id is empty, it always returns false.
func (x *Error) Is(target error) bool {
var err *Error
if errors.As(target, &err) {
if x.id != "" && x.id == err.id {
return true
}
}
return x == target
}
// ID sets string to check equality in Error.IS()
func (x *Error) ID(id string) *Error {
x.id = id
return x
}
// Wrap creates a new Error and copy message and id to new one.
func (x *Error) Wrap(cause error) *Error {
err := newError()
x.copy(err)
err.cause = cause
return err
}
// Values returns map of key and value that is set by With. All wrapped goerr.Error key and values will be merged. Key and values of wrapped error is overwritten by upper goerr.Error.
func (x *Error) Values() map[string]any {
var values map[string]any
if cause := x.Unwrap(); cause != nil {
if err, ok := cause.(*Error); ok {
values = err.Values()
}
}
if values == nil {
values = make(map[string]any)
}
for key, value := range x.values {
values[key] = value
}
return values
}
func (x *Error) LogValue() slog.Value {
if x == nil {
return slog.AnyValue(nil)
}
attrs := []slog.Attr{
slog.String("message", x.msg),
}
var values []any
for k, v := range x.values {
values = append(values, slog.Any(k, v))
}
attrs = append(attrs, slog.Group("values", values...))
var stacktrace any
var traces []string
for _, st := range x.StackTrace() {
traces = append(traces, fmt.Sprintf("%s:%d %s", st.file(), st.line(), st.name()))
}
stacktrace = traces
attrs = append(attrs, slog.Any("stacktrace", stacktrace))
if x.cause != nil {
var errAttr slog.Attr
if lv, ok := x.cause.(slog.LogValuer); ok {
errAttr = slog.Any("cause", lv.LogValue())
} else {
errAttr = slog.Any("cause", x.cause)
}
attrs = append(attrs, errAttr)
}
return slog.GroupValue(attrs...)
}