-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
builder.go
361 lines (303 loc) · 7.98 KB
/
builder.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
package oops
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/oklog/ulid/v2"
"github.com/samber/lo"
"go.opentelemetry.io/otel/trace"
)
/**
* Builder pattern.
*
* oops.Errorf("Could not fetch users: %w", err)
*
* oops.
* User("steve@apple.com", "firstname", "Samuel").
* Tenant("apple", "country", "us").
* Errorf("403 not permitted")
*
* oops.
* Time(requestDate).
* Duration(requestDuration).
* Tx(traceID).
* Errorf("Failed to execute http request")
*
* oops.
* With("project_id", project.ID, "created_at", project.CreatedAt).
* Errorf("Could not update settings")
*
*/
type OopsErrorBuilder OopsError
func new() OopsErrorBuilder {
return OopsErrorBuilder{
err: nil,
msg: "",
code: "",
time: time.Now(),
duration: 0,
// context
domain: "",
tags: []string{},
context: map[string]any{},
trace: "",
span: "",
hint: "",
public: "",
owner: "",
// user
userID: "",
userData: map[string]any{},
tenantID: "",
tenantData: map[string]any{},
// http
req: nil,
res: nil,
// stacktrace
stacktrace: nil,
}
}
func (o OopsErrorBuilder) copy() OopsErrorBuilder {
return OopsErrorBuilder{
// err: err,
// msg: o.msg,
code: o.code,
time: o.time,
duration: o.duration,
domain: o.domain,
tags: o.tags,
context: lo.Assign(map[string]any{}, o.context),
trace: o.trace,
span: o.span,
hint: o.hint,
public: o.public,
owner: o.owner,
userID: o.userID,
userData: lo.Assign(map[string]any{}, o.userData),
tenantID: o.tenantID,
tenantData: lo.Assign(map[string]any{}, o.tenantData),
req: o.req,
res: o.res,
// stacktrace: o.stacktrace,
}
}
// Wrap wraps an error into an `oops.OopsError` object that satisfies `error`
func (o OopsErrorBuilder) Wrap(err error) error {
if err == nil {
return nil
}
o2 := o.copy()
o2.err = err
if o2.span == "" {
o2.span = ulid.Make().String()
}
o2.stacktrace = newStacktrace(o2.span)
return OopsError(o2)
}
// Wrapf wraps an error into an `oops.OopsError` object that satisfies `error` and formats an error message.
func (o OopsErrorBuilder) Wrapf(err error, format string, args ...any) error {
if err == nil {
return nil
}
o2 := o.copy()
o2.err = err
o2.msg = fmt.Errorf(format, args...).Error()
if o2.span == "" {
o2.span = ulid.Make().String()
}
o2.stacktrace = newStacktrace(o2.span)
return OopsError(o2)
}
// Errorf formats an error and returns `oops.OopsError` object that satisfies `error`.
func (o OopsErrorBuilder) Errorf(format string, args ...any) error {
o2 := o.copy()
o2.err = fmt.Errorf(format, args...)
if o2.span == "" {
o2.span = ulid.Make().String()
}
o2.stacktrace = newStacktrace(o2.span)
return OopsError(o2)
}
func (o OopsErrorBuilder) Join(e ...error) error {
return o.Wrap(errors.Join(e...))
}
// Recover handle panic and returns `oops.OopsError` object that satisfies `error`.
func (o OopsErrorBuilder) Recover(cb func()) (err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
err = o.Wrap(e)
} else {
err = o.Wrap(fmt.Errorf("%v", r))
}
}
}()
cb()
return
}
// Recoverf handle panic and returns `oops.OopsError` object that satisfies `error` and formats an error message.
func (o OopsErrorBuilder) Recoverf(cb func(), msg string, args ...any) (err error) {
return o.Wrapf(o.Recover(cb), msg, args...)
}
// Assert panics if condition is false. Panic payload will be of type oops.OopsError.
// Assertions can be chained.
func (o OopsErrorBuilder) Assert(condition bool) OopsErrorBuilder {
if !condition {
panic(o.Errorf("assertion failed"))
}
return o // no need to copy
}
// Assertf panics if condition is false. Panic payload will be of type oops.OopsError.
// Assertions can be chained.
func (o OopsErrorBuilder) Assertf(condition bool, msg string, args ...any) OopsErrorBuilder {
if !condition {
panic(o.Errorf(msg, args...))
}
return o // no need to copy
}
// Code set a code or slug that describes the error.
// Error messages are intented to be read by humans, but such code is expected to
// be read by machines and even transported over different services.
func (o OopsErrorBuilder) Code(code string) OopsErrorBuilder {
o2 := o.copy()
o2.code = code
return o2
}
// Time set the error time.
// Default: `time.Now()`
func (o OopsErrorBuilder) Time(time time.Time) OopsErrorBuilder {
o2 := o.copy()
o2.time = time
return o2
}
// Since set the error duration.
func (o OopsErrorBuilder) Since(t time.Time) OopsErrorBuilder {
o2 := o.copy()
o2.duration = time.Since(t)
return o2
}
// Duration set the error duration.
func (o OopsErrorBuilder) Duration(duration time.Duration) OopsErrorBuilder {
o2 := o.copy()
o2.duration = duration
return o2
}
// In set the feature category or domain.
func (o OopsErrorBuilder) In(domain string) OopsErrorBuilder {
o2 := o.copy()
o2.domain = domain
return o2
}
// Tags adds multiple tags, describing the feature returning an error.
func (o OopsErrorBuilder) Tags(tags ...string) OopsErrorBuilder {
o2 := o.copy()
o2.tags = append(o2.tags, tags...)
return o2
}
// With supplies a list of attributes declared by pair of key+value.
func (o OopsErrorBuilder) With(kv ...any) OopsErrorBuilder {
o2 := o.copy()
for i := 0; i < len(kv)-1; i += 2 {
k := kv[i]
v := kv[i+1]
if key, ok := k.(string); ok {
o2.context[key] = v
}
}
return o2
}
// WithContext supplies a list of values declared in context.
func (o OopsErrorBuilder) WithContext(ctx context.Context, keys ...any) OopsErrorBuilder {
o2 := o.copy()
for i := 0; i < len(keys); i++ {
switch k := keys[i].(type) {
case fmt.Stringer:
o2.context[k.String()] = contextValueOrNil(ctx, k.String())
case string:
o2.context[k] = contextValueOrNil(ctx, k)
case *string:
o2.context[*k] = contextValueOrNil(ctx, *k)
default:
o2.context[fmt.Sprint(k)] = contextValueOrNil(ctx, k)
}
}
spanCtx := trace.SpanContextFromContext(ctx)
if spanCtx.HasTraceID() {
o2.trace = spanCtx.TraceID().String()
}
if spanCtx.HasSpanID() {
o2.span = spanCtx.SpanID().String()
}
return o2
}
// Trace set a transaction id, trace id or correlation id...
func (o OopsErrorBuilder) Trace(trace string) OopsErrorBuilder {
o2 := o.copy()
o2.trace = trace
return o2
}
// Span represents a unit of work or operation.
func (o OopsErrorBuilder) Span(span string) OopsErrorBuilder {
o2 := o.copy()
o2.span = span
return o2
}
// Hint set a hint for faster debugging.
func (o OopsErrorBuilder) Hint(hint string) OopsErrorBuilder {
o2 := o.copy()
o2.hint = hint
return o2
}
// Public represents a message that is safe to be shown to an end-user.
func (o OopsErrorBuilder) Public(public string) OopsErrorBuilder {
o2 := o.copy()
o2.public = public
return o2
}
// Owner set the name/email of the collegue/team responsible for handling this error.
// Useful for alerting purpose.
func (o OopsErrorBuilder) Owner(owner string) OopsErrorBuilder {
o2 := o.copy()
o2.owner = owner
return o2
}
// User supplies user id and a chain of key/value.
func (o OopsErrorBuilder) User(userID string, userData ...any) OopsErrorBuilder {
o2 := o.copy()
o2.userID = userID
for i := 0; i < len(userData)-1; i += 2 {
k := userData[i]
v := userData[i+1]
if key, ok := k.(string); ok {
o2.userData[key] = v
}
}
return o2
}
// Tenant supplies tenant id and a chain of key/value.
func (o OopsErrorBuilder) Tenant(tenantID string, tenantData ...any) OopsErrorBuilder {
o2 := o.copy()
o2.tenantID = tenantID
for i := 0; i < len(tenantData)-1; i += 2 {
k := tenantData[i]
v := tenantData[i+1]
if key, ok := k.(string); ok {
o2.tenantData[key] = v
}
}
return o2
}
// Request supplies a http.Request.
func (o OopsErrorBuilder) Request(req *http.Request, withBody bool) OopsErrorBuilder {
o2 := o.copy()
o2.req = lo.ToPtr(lo.T2(req, withBody))
return o2
}
// Response supplies a http.Response.
func (o OopsErrorBuilder) Response(res *http.Response, withBody bool) OopsErrorBuilder {
o2 := o.copy()
o2.res = lo.ToPtr(lo.T2(res, withBody))
return o2
}