From 116c8060e034e8d46855354d22db2acbc8df9e1e Mon Sep 17 00:00:00 2001 From: Ali Asghar <98263017+alliasgher@users.noreply.github.com> Date: Tue, 21 Apr 2026 04:17:57 +0500 Subject: [PATCH] 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 --- event.go | 3 ++- event_test.go | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/event.go b/event.go index 7538e66..8c9c523 100644 --- a/event.go +++ b/event.go @@ -466,7 +466,8 @@ func (e *Event) Err(err error) *Event { if e.stack && ErrorStackMarshaler != nil { switch m := ErrorStackMarshaler(err).(type) { 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: e = e.Object(ErrorStackFieldName, m) case error: diff --git a/event_test.go b/event_test.go index 1446947..08ed5ea 100644 --- a/event_test.go +++ b/event_test.go @@ -720,7 +720,13 @@ func TestEvent_ErrWithStackMarshalerNil(t *testing.T) { log.Log().Stack().Err(err).Msg("test message") 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 { t.Errorf("Event.Err() with nil stack marshaler = %q, want %q", got, want) }