forked from samber/oops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
578 lines (520 loc) · 15.9 KB
/
builder.go
File metadata and controls
578 lines (520 loc) · 15.9 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
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 implementation for creating rich error objects.
*
* The builder pattern allows for fluent, chainable error creation with contextual
* information. This design provides a clean API for building complex error objects
* while maintaining readability and flexibility.
*
* Examples:
*
* Basic error creation:
* oops.Errorf("Could not fetch users: %w", err)
*
* Rich error with context:
* oops.
* User("steve@apple.com", "firstname", "Samuel").
* Tenant("apple", "country", "us").
* Errorf("403 not permitted")
*
* Error with timing and tracing:
* oops.
* Time(requestDate).
* Duration(requestDuration).
* Trace(traceID).
* Errorf("Failed to execute http request")
*
* Error with custom context:
* oops.
* With("project_id", project.ID, "created_at", project.CreatedAt).
* Errorf("Could not update settings")
*
* Thread Safety: OopsErrorBuilder instances are not thread-safe. Each builder
* should be used by a single goroutine. The builder methods return new instances
* to support chaining, which provides some isolation but doesn't guarantee
* thread safety for concurrent access to the same builder instance.
*/
// OopsErrorBuilder implements the builder pattern for creating OopsError instances.
// It provides a fluent API for setting error attributes and creating error objects.
// The builder is designed to be chainable, allowing multiple method calls in sequence.
type OopsErrorBuilder OopsError
// new creates a new OopsErrorBuilder with default values.
// This function initializes all fields to their zero values except for time,
// which is set to the current time.
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,
}
}
// copy creates a deep copy of the current builder state.
// This method is used internally to create new builder instances for chaining.
// It performs deep copying of maps to ensure that modifications to the new
// builder don't affect the original.
func (o OopsErrorBuilder) copy() OopsErrorBuilder {
return OopsErrorBuilder{
// err: err, // Not copied as it's set by error creation methods
// msg: o.msg, // Not copied as it's set by error creation methods
code: o.code,
time: o.time,
duration: o.duration,
domain: o.domain,
tags: o.tags,
context: lo.Assign(map[string]any{}, o.context), // Deep copy of context map (pointer values are not copied)
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), // Deep copy of user data (pointer values are not copied)
tenantID: o.tenantID,
tenantData: lo.Assign(map[string]any{}, o.tenantData), // Deep copy of tenant data (pointer values are not copied)
req: o.req,
res: o.res,
// stacktrace: o.stacktrace, // Not copied as it's generated per error
}
}
// Wrap wraps an existing error into an OopsError with the current builder's context.
// If the input error is nil, returns nil. Otherwise, creates a new OopsError that
// wraps the original error while preserving all the contextual information set
// in the builder.
//
// Example:
//
// err := oops.
// Code("database_error").
// In("database").
// Wrap(originalError)
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() // Generate unique span ID if not set
}
o2.stacktrace = newStacktrace(o2.span) // Capture stack trace at error creation
return OopsError(o2)
}
// Wrapf wraps an existing error with additional formatted message.
// Similar to Wrap, but adds a formatted message that describes the context
// in which the error occurred.
//
// Example:
//
// err := oops.
// Code("database_error").
// In("database").
// Wrapf(originalError, "failed to execute query: %s", queryName)
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() // Format the additional message
if o2.span == "" {
o2.span = ulid.Make().String()
}
o2.stacktrace = newStacktrace(o2.span)
return OopsError(o2)
}
// New creates a new error with the specified message.
// This method creates a simple error without wrapping an existing one.
// The message is treated as the primary error message.
//
// Example:
//
// err := oops.
// Code("validation_error").
// New("invalid input parameters")
func (o OopsErrorBuilder) New(message string) error {
o2 := o.copy()
o2.err = errors.New(message)
if o2.span == "" {
o2.span = ulid.Make().String()
}
o2.stacktrace = newStacktrace(o2.span)
return OopsError(o2)
}
// Errorf creates a new error with a formatted message.
// Similar to New, but allows for formatted messages using printf-style formatting.
//
// Example:
//
// err := oops.
// Code("validation_error").
// Errorf("invalid input: expected %s, got %s", expectedType, actualType)
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)
}
// Join combines multiple errors into a single error.
// This method uses the standard errors.Join function to combine multiple
// errors while preserving the builder's context.
//
// Example:
//
// err := oops.
// Code("multi_error").
// Join(err1, err2, err3)
func (o OopsErrorBuilder) Join(e ...error) error {
return o.Wrap(errors.Join(e...))
}
// Recover handles panics and converts them to OopsError instances.
// This method executes the provided callback function and catches any panics,
// converting them to properly formatted OopsError instances with stack traces.
// If the panic payload is already an error, it wraps that error. Otherwise,
// it creates a new error from the panic value.
//
// Example:
//
// err := oops.
// Code("panic_recovered").
// Recover(func() {
// // Potentially panicking code
// riskyOperation()
// })
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) // Wrap existing error
} else {
err = o.Wrap(fmt.Errorf("%v", r)) // Convert panic value to error
}
}
}()
cb()
return
}
// Recoverf handles panics with additional context message.
// Similar to Recover, but adds a formatted message to describe the context
// in which the panic occurred.
//
// Example:
//
// err := oops.
// Code("panic_recovered").
// Recoverf(func() {
// riskyOperation()
// }, "panic in operation: %s", operationName)
func (o OopsErrorBuilder) Recoverf(cb func(), msg string, args ...any) (err error) {
return o.Wrapf(o.Recover(cb), msg, args...)
}
// Assert panics if the condition is false.
// This method provides a way to add assertions to code that will panic
// with an OopsError if the condition fails. The assertion can be chained
// with other builder methods.
//
// Example:
//
// oops.
// Code("assertion_failed").
// Assert(userID != "", "user ID cannot be empty").
// Assert(email != "", "user email cannot be empty").
// Assert(orgID != "", "user organization ID cannot be empty")
func (o OopsErrorBuilder) Assert(condition bool) OopsErrorBuilder {
if !condition {
panic(o.Errorf("assertion failed"))
}
return o // Return self for chaining
}
// Assertf panics if the condition is false with a custom message.
// Similar to Assert, but allows for a custom formatted message when
// the assertion fails.
//
// Example:
//
// oops.
// Code("assertion_failed").
// Assertf(userID != "", "user ID cannot be empty, got: %s", userID).
// Assertf(email != "", "user email cannot be empty, got: %s", email).
// Assertf(orgID != "", "user organization ID cannot be empty, got: %s", orgID)
func (o OopsErrorBuilder) Assertf(condition bool, msg string, args ...any) OopsErrorBuilder {
if !condition {
panic(o.Errorf(msg, args...))
}
return o // Return self for chaining
}
// Code sets a machine-readable error code or slug.
// Error codes are useful for programmatic error handling and cross-service
// error correlation. They should be consistent and well-documented.
//
// Example:
//
// oops.Code("database_connection_failed").Errorf("connection timeout")
func (o OopsErrorBuilder) Code(code string) OopsErrorBuilder {
o2 := o.copy()
o2.code = code
return o2
}
// Time sets the timestamp when the error occurred.
// If not set, the error will use the current time when created.
//
// Example:
//
// oops.Time(time.Now()).Errorf("operation failed")
func (o OopsErrorBuilder) Time(time time.Time) OopsErrorBuilder {
o2 := o.copy()
o2.time = time
return o2
}
// Since calculates the duration since the specified time.
// This is useful for measuring how long an operation took before failing.
//
// Example:
//
// start := time.Now()
// // ... perform operation ...
// oops.Since(start).Errorf("operation timed out")
func (o OopsErrorBuilder) Since(t time.Time) OopsErrorBuilder {
o2 := o.copy()
o2.duration = time.Since(t)
return o2
}
// Duration sets the duration associated with the error.
// This is useful for errors that are related to timeouts or performance issues.
//
// Example:
//
// oops.Duration(5 * time.Second).Errorf("request timeout")
func (o OopsErrorBuilder) Duration(duration time.Duration) OopsErrorBuilder {
o2 := o.copy()
o2.duration = duration
return o2
}
// In sets the domain or feature category for the error.
// Domains help categorize errors by the part of the system they relate to.
//
// Example:
//
// oops.In("database").Errorf("connection failed")
func (o OopsErrorBuilder) In(domain string) OopsErrorBuilder {
o2 := o.copy()
o2.domain = domain
return o2
}
// Tags adds multiple tags for categorizing the error.
// Tags are useful for filtering and grouping errors in monitoring systems.
//
// Example:
//
// oops.Tags("auth", "permission", "critical").Errorf("access denied")
func (o OopsErrorBuilder) Tags(tags ...string) OopsErrorBuilder {
o2 := o.copy()
o2.tags = append(o2.tags, tags...)
return o2
}
// With adds key-value pairs to the error context.
// Context values are useful for debugging and provide additional information
// about the error. Values can be of any type and will be serialized appropriately.
//
// Performance: Context values are stored in a map and processed during error
// creation. Large numbers of context values may impact performance later, but not
// during error creation.
//
// Example:
//
// oops.With("user_id", 123, "operation", "create").Errorf("validation failed")
func (o OopsErrorBuilder) With(kv ...any) OopsErrorBuilder {
o2 := o.copy()
// Process key-value pairs in chunks of 2
for i := 0; i < len(kv); i += 2 {
if i+1 < len(kv) {
key, ok := kv[i].(string)
if ok {
o2.context[key] = kv[i+1]
}
}
}
return o2
}
// WithContext extracts values from a Go context and adds them to the error context.
// This is useful for propagating context values through error chains.
//
// Example:
//
// oops.WithContext(ctx, "request_id", "user_id").Errorf("operation failed")
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 sets a transaction, trace, or correlation ID.
// This is useful for distributed tracing and correlating errors across services.
//
// Example:
//
// oops.Trace("req-123-456").Errorf("service call failed")
func (o OopsErrorBuilder) Trace(trace string) OopsErrorBuilder {
o2 := o.copy()
o2.trace = trace
return o2
}
// Span sets the current span identifier.
// Spans represent units of work and are useful for distributed tracing.
//
// Example:
//
// oops.Span("database-query").Errorf("query failed")
func (o OopsErrorBuilder) Span(span string) OopsErrorBuilder {
o2 := o.copy()
o2.span = span
return o2
}
// Hint provides a debugging hint for resolving the error.
// Hints should provide actionable guidance for developers.
//
// Example:
//
// oops.Hint("Check database connection and credentials").Errorf("connection failed")
func (o OopsErrorBuilder) Hint(hint string) OopsErrorBuilder {
o2 := o.copy()
o2.hint = hint
return o2
}
// Public sets a user-safe error message.
// This message should be safe to display to end users without exposing
// internal system details.
//
// Example:
//
// oops.Public("Unable to process your request").Errorf("internal server error")
func (o OopsErrorBuilder) Public(public string) OopsErrorBuilder {
o2 := o.copy()
o2.public = public
return o2
}
// Owner sets the person or team responsible for handling this error.
// This is useful for alerting and error routing.
//
// Example:
//
// oops.Owner("database-team@company.com").Errorf("connection failed")
func (o OopsErrorBuilder) Owner(owner string) OopsErrorBuilder {
o2 := o.copy()
o2.owner = owner
return o2
}
// User adds user information to the error context.
// This method accepts a user ID followed by key-value pairs for user data.
//
// Example:
//
// oops.User("user-123", "firstname", "John", "lastname", "Doe").Errorf("permission denied")
func (o OopsErrorBuilder) User(userID string, userData ...any) OopsErrorBuilder {
o2 := o.copy()
o2.userID = userID
// Process user data key-value pairs
for i := 0; i < len(userData); i += 2 {
if i+1 < len(userData) {
key, ok := userData[i].(string)
if ok {
o2.userData[key] = userData[i+1]
}
}
}
return o2
}
// Tenant adds tenant information to the error context.
// This method accepts a tenant ID followed by key-value pairs for tenant data.
//
// Example:
//
// oops.Tenant("tenant-456", "name", "Acme Corp", "plan", "premium").Errorf("quota exceeded")
func (o OopsErrorBuilder) Tenant(tenantID string, tenantData ...any) OopsErrorBuilder {
o2 := o.copy()
o2.tenantID = tenantID
// Process tenant data key-value pairs
for i := 0; i < len(tenantData); i += 2 {
if i+1 < len(tenantData) {
key, ok := tenantData[i].(string)
if ok {
o2.tenantData[key] = tenantData[i+1]
}
}
}
return o2
}
// Request adds HTTP request information to the error context.
// The withBody parameter controls whether the request body is included.
// Including request bodies may impact performance and memory usage.
//
// Example:
//
// oops.Request(req, true).Errorf("request processing failed")
func (o OopsErrorBuilder) Request(req *http.Request, withBody bool) OopsErrorBuilder {
o2 := o.copy()
o2.req = lo.ToPtr(lo.T2(req, withBody))
return o2
}
// Response adds HTTP response information to the error context.
// The withBody parameter controls whether the response body is included.
// Including response bodies may impact performance and memory usage.
//
// Example:
//
// oops.Response(res, false).Errorf("response processing failed")
func (o OopsErrorBuilder) Response(res *http.Response, withBody bool) OopsErrorBuilder {
o2 := o.copy()
o2.res = lo.ToPtr(lo.T2(res, withBody))
return o2
}