1
0
mirror of https://github.com/rs/zerolog synced 2026-06-08 17:13:30 +00:00

event: restore Err() logging when ErrorStackMarshaler returns nil (#763)

Commit f6fbd33 added `case nil: return e` in Event.Err() so that when
ErrorStackMarshaler returns nil (the error has no stack trace), the
function returns early without logging the error itself.

This was a regression. The previous behaviour was to fall through the
switch and always call AnErr(ErrorFieldName, err), ensuring the error
message is logged even when no stack is attached.

Libraries such as elastic/ecs-logging-go-zerolog (and plain fmt.Errorf
errors) rely on the old behaviour: calling Stack().Err(err) should
always log the error field; the stack field is additional and optional.

Change the `nil` case from `return e` to a comment (fall-through) so
that AnErr is called unconditionally. Update the test that was written
to match the incorrect behaviour.

Fixes #762

Signed-off-by: alliasgher <alliasgher123@gmail.com>
This commit is contained in:
Ali Asghar
2026-04-21 04:17:57 +05:00
committed by GitHub
parent 13966551e7
commit 116c8060e0
2 changed files with 9 additions and 2 deletions
+2 -1
View File
@@ -466,7 +466,8 @@ func (e *Event) Err(err error) *Event {
if e.stack && ErrorStackMarshaler != nil { if e.stack && ErrorStackMarshaler != nil {
switch m := ErrorStackMarshaler(err).(type) { switch m := ErrorStackMarshaler(err).(type) {
case nil: case nil:
return e // ErrorStackMarshaler returned nil — the error has no stack trace to
// attach. Fall through and still log the error via AnErr below.
case LogObjectMarshaler: case LogObjectMarshaler:
e = e.Object(ErrorStackFieldName, m) e = e.Object(ErrorStackFieldName, m)
case error: case error:
+7 -1
View File
@@ -720,7 +720,13 @@ func TestEvent_ErrWithStackMarshalerNil(t *testing.T) {
log.Log().Stack().Err(err).Msg("test message") log.Log().Stack().Err(err).Msg("test message")
got := buf.String() got := buf.String()
want := `{"message":"test message"}` + "\n" // No fields because stack marshaler returned nil // When ErrorStackMarshaler returns nil (no stack trace available for this
// error), Err() must still log the error value via AnErr. Without a stack
// field, the output is the same as if Stack() had not been called.
// Regression test for https://github.com/rs/zerolog/issues/762:
// commit f6fbd33 introduced a `case nil: return e` branch that silently
// swallowed the error instead of falling through to AnErr.
want := `{"error":"test error","message":"test message"}` + "\n"
if got != want { if got != want {
t.Errorf("Event.Err() with nil stack marshaler = %q, want %q", got, want) t.Errorf("Event.Err() with nil stack marshaler = %q, want %q", got, want)
} }