Skip to content

fix: Err() should record error when ErrorStackMarshaler returns nil#764

Closed
Yanhu007 wants to merge 2 commits into
rs:masterfrom
Yanhu007:fix/err-nil-stack-marshaler
Closed

fix: Err() should record error when ErrorStackMarshaler returns nil#764
Yanhu007 wants to merge 2 commits into
rs:masterfrom
Yanhu007:fix/err-nil-stack-marshaler

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #762

Problem

Since commit f6fbd33, when ErrorStackMarshaler returns nil (e.g. for errors that don't have stack traces), Err() returns immediately without calling AnErr(ErrorFieldName, err). This silently drops the error message from log output.

Before the regression:

{"level":"error","error":"something failed","message":"..."}

After the regression:

{"level":"error","message":"..."}

(error field is missing!)

Fix

Change case nil: return e to an empty case nil: so control falls through to the AnErr() call that records the error field. The stack field is correctly skipped when the marshaler returns nil.

When ErrorStackMarshaler returns nil (e.g. for errors without stack
traces), Err() returned the event without recording the error field.
This caused error messages to silently disappear from log output.

The nil case should skip the stack field but still fall through to
AnErr() to record the error itself.

Regression introduced in f6fbd33.

Fixes rs#762
Copilot AI review requested due to automatic review settings April 15, 2026 09:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression where Event.Err() could drop the error field when ErrorStackMarshaler returns nil (e.g., errors without stack traces), while still allowing stack output when available.

Changes:

  • Adjust Event.Err() stack-marshaling switch so a nil stack-marshaler result skips only the stack field (and does not short-circuit the error field recording).
Comments suppressed due to low confidence (1)

event.go:483

  • This change fixes Event.Err() when ErrorStackMarshaler returns nil, but the same early-return behavior still exists in Context.Err() (context.go:210-226) and in stack handling inside appendFieldList (fields.go:82-99). If the intent of this PR/issue is that Err() should always still record the error field when the stack marshaler returns nil, those call sites should be updated too; otherwise the behavior will remain inconsistent depending on whether the error is added via an Event, via Logger.With().Err(...), or via Fields(...). Also note there are existing tests that currently assert the old behavior (e.g. TestEvent_ErrWithStackMarshalerNil, TestContext_ErrWithNilStackMarshaler) and will need to be updated to match the new contract.
	if e.stack && ErrorStackMarshaler != nil {
		switch m := ErrorStackMarshaler(err).(type) {
		case nil:
			// Stack marshaler returned nil; skip the stack field
			// but still record the error below.
		case LogObjectMarshaler:
			e = e.Object(ErrorStackFieldName, m)
		case error:
			e = e.Str(ErrorStackFieldName, m.Error())
		case string:
			e = e.Str(ErrorStackFieldName, m)
		default:
			e = e.Interface(ErrorStackFieldName, m)
		}
	}

	return e.AnErr(ErrorFieldName, err)
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Apply the same fix to Context.Err() which had the identical bug:
case nil returned early without recording the error field.

Spotted by code review feedback.
@Yanhu007
Copy link
Copy Markdown
Author

Good catch on Context.Err() having the same issue! I've pushed an additional commit that fixes the identical case nil: return c early return in context.go as well.

Both Event.Err() and Context.Err() now correctly skip only the stack field when the marshaler returns nil, while still recording the error itself.

@IDisposable
Copy link
Copy Markdown
Contributor

This one doesn't update the unit tests, probably should prefer #758

@IDisposable
Copy link
Copy Markdown
Contributor

#763 was merged so this should be closed.

@rs rs closed this Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Behavior change on Event.Err with commit f6fbd33

4 participants