@@ -61,7 +61,7 @@ func Wrap(err error, args ...interface{}) Error {
61
61
trace = newTrace (err , 2 )
62
62
}
63
63
if len (args ) > 0 {
64
- trace = trace . AddUserMessage ( args [0 ], args [1 :]... )
64
+ trace = WithUserMessage ( trace , args [0 ], args [1 :]... )
65
65
}
66
66
return trace
67
67
}
@@ -172,7 +172,7 @@ func WrapWithMessage(err error, message interface{}, args ...interface{}) Error
172
172
} else {
173
173
trace = newTrace (err , 2 )
174
174
}
175
- return trace . AddUserMessage ( message , args ... )
175
+ return WithUserMessage ( trace , message , args ... )
176
176
}
177
177
178
178
// Errorf is similar to fmt.Errorf except that it captures
@@ -253,7 +253,7 @@ type RawTrace struct {
253
253
Fields map [string ]interface {} `json:"fields,omitempty"`
254
254
}
255
255
256
- func (e * TraceErr ) clone () * TraceErr {
256
+ func (e * TraceErr ) Clone () * TraceErr {
257
257
if e == nil {
258
258
return nil
259
259
}
@@ -278,36 +278,6 @@ func (e *TraceErr) clone() *TraceErr {
278
278
return tr
279
279
}
280
280
281
- // AddUserMessage adds user-friendly message describing the error nature
282
- func (e * TraceErr ) AddUserMessage (formatArg interface {}, rest ... interface {}) * TraceErr {
283
- newMessage := fmt .Sprintf (fmt .Sprintf ("%v" , formatArg ), rest ... )
284
- errClone := e .clone ()
285
- errClone .Messages = append (errClone .Messages , newMessage )
286
- return errClone
287
- }
288
-
289
- // AddFields adds the given map of fields to the error being reported
290
- func (e * TraceErr ) AddFields (fields map [string ]interface {}) * TraceErr {
291
- errClone := e .clone ()
292
- if errClone .Fields == nil {
293
- errClone .Fields = make (map [string ]interface {}, len (fields ))
294
- }
295
- for k , v := range fields {
296
- errClone .Fields [k ] = v
297
- }
298
- return errClone
299
- }
300
-
301
- // AddField adds a single field to the error wrapper as context for the error
302
- func (e * TraceErr ) AddField (k string , v interface {}) * TraceErr {
303
- errClone := e .clone ()
304
- if errClone .Fields == nil {
305
- errClone .Fields = make (map [string ]interface {}, 1 )
306
- }
307
- errClone .Fields [k ] = v
308
- return errClone
309
- }
310
-
311
281
// UserMessage returns user-friendly error message
312
282
func (e * TraceErr ) UserMessage () string {
313
283
if len (e .Messages ) > 0 {
@@ -383,7 +353,7 @@ func (e *TraceErr) OrigError() error {
383
353
}
384
354
385
355
// GoString formats this trace object for use with
386
- // with the "%#v" format string
356
+ // the "%#v" format string
387
357
func (e * TraceErr ) GoString () string {
388
358
return e .DebugReport ()
389
359
}
@@ -402,21 +372,48 @@ type Error interface {
402
372
DebugReporter
403
373
UserMessager
404
374
405
- // AddUserMessage adds formatted user-facing message
406
- // to the error, depends on the implementation,
407
- // usually works as fmt.Sprintf(formatArg, rest...)
408
- // but implementations can choose another way, e.g. treat
409
- // arguments as structured args
410
- AddUserMessage ( formatArg interface {}, rest ... interface {}) * TraceErr
375
+ // GetFields returns any fields that have been added to the error
376
+ GetFields () map [ string ] interface {}
377
+
378
+ // Clone returns a copy of the current Error.
379
+ Clone () * TraceErr
380
+ }
411
381
412
- // AddField adds additional field information to the error
413
- AddField (key string , value interface {}) * TraceErr
382
+ // WithUserMessage adds formatted user-facing message
383
+ // to the error, depends on the implementation,
384
+ // usually works as fmt.Sprintf(formatArg, rest...)
385
+ // but implementations can choose another way, e.g. treat
386
+ // arguments as structured args.
387
+ func WithUserMessage (err Error , formatArg interface {}, rest ... interface {}) * TraceErr {
388
+ errCopy := err .Clone ()
414
389
415
- // AddFields adds a map of additional fields to the error
416
- AddFields (fields map [string ]interface {}) * TraceErr
390
+ newMessage := fmt .Sprintf (fmt .Sprintf ("%v" , formatArg ), rest ... )
391
+ errCopy .Messages = append (errCopy .Messages , newMessage )
392
+ return errCopy
393
+ }
417
394
418
- // GetFields returns any fields that have been added to the error
419
- GetFields () map [string ]interface {}
395
+ // WithField adds additional field information to the error.
396
+ func WithField (err Error , key string , value interface {}) * TraceErr {
397
+ errCopy := err .Clone ()
398
+
399
+ if errCopy .Fields == nil {
400
+ errCopy .Fields = make (map [string ]interface {}, 1 )
401
+ }
402
+ errCopy .Fields [key ] = value
403
+ return errCopy
404
+ }
405
+
406
+ // WithFields adds a map of additional fields to the error
407
+ func WithFields (err Error , fields map [string ]interface {}) * TraceErr {
408
+ errCopy := err .Clone ()
409
+
410
+ if errCopy .Fields == nil {
411
+ errCopy .Fields = make (map [string ]interface {}, len (fields ))
412
+ }
413
+ for k , v := range fields {
414
+ errCopy .Fields [k ] = v
415
+ }
416
+ return errCopy
420
417
}
421
418
422
419
// NewAggregate creates a new aggregate instance from the specified
0 commit comments