MCPcopy
hub / github.com/rs/zerolog / Err

Method Err

event.go:461–483  ·  view source on GitHub ↗

Err adds the field "error" with serialized err to the *Event context. If err is nil, no field is added. To customize the key name, change zerolog.ErrorFieldName. If Stack() has been called before and zerolog.ErrorStackMarshaler is defined, the err is passed to ErrorStackMarshaler and the result is

(err error)

Source from the content-addressed store, hash-verified

459// the err is passed to ErrorStackMarshaler and the result is appended to the
460// zerolog.ErrorStackFieldName.
461func (e *Event) Err(err error) *Event {
462 if e == nil {
463 return e
464 }
465
466 if e.stack && ErrorStackMarshaler != nil {
467 switch m := ErrorStackMarshaler(err).(type) {
468 case nil:
469 // ErrorStackMarshaler returned nil — the error has no stack trace to
470 // attach. Fall through and still log the error via AnErr below.
471 case LogObjectMarshaler:
472 e = e.Object(ErrorStackFieldName, m)
473 case error:
474 e = e.Str(ErrorStackFieldName, m.Error())
475 case string:
476 e = e.Str(ErrorStackFieldName, m)
477 default:
478 e = e.Interface(ErrorStackFieldName, m)
479 }
480 }
481
482 return e.AnErr(ErrorFieldName, err)
483}
484
485// Stack enables stack trace printing for the error passed to Err().
486//

Callers 1

TestEvent_WithNilEventFunction · 0.95

Calls 5

ObjectMethod · 0.95
StrMethod · 0.95
InterfaceMethod · 0.95
AnErrMethod · 0.95
ErrorMethod · 0.45

Tested by 1

TestEvent_WithNilEventFunction · 0.76