Skip to content

entry: allow function-backed error fields - #1579

Merged
thaJeztah merged 2 commits into
sirupsen:masterfrom
thaJeztah:err_handling
Aug 18, 2026
Merged

thaJeztah merged 2 commits into
sirupsen:masterfrom
thaJeztah:err_handling

Conversation

@thaJeztah

Copy link
Copy Markdown
Collaborator

entry: allow function-backed error fields

Treat values implementing error as supported fields before checking their
underlying kind.

This allows function-backed error implementations to be added through
WithError, WithField, and WithFields, matching formatter behavior that renders
error values through Error(), while continuing to reject unsupported function
values.

logger: WithError: wrap logger.WithError for consistency

The utility was skipping WithError as intermediate; wrap the logger's
WithError method instead.

Performance remains identical, or even slightly improved for some cases;

pkg: github.com/sirupsen/logrus/benches
cpu: Apple M3 Pro
                           │ before.txt  │             after.txt              │
                           │   sec/op    │   sec/op     vs base               │
Entry_WithError/WithError    121.2n ± 1%   121.7n ± 1%       ~ (p=0.984 n=20)
Entry_WithError/WithField    123.7n ± 1%   121.0n ± 1%  -2.14% (p=0.000 n=20)
Entry_WithError/WithFields   212.5n ± 1%   209.3n ± 1%  -1.51% (p=0.000 n=20)
geomean                      147.2n        145.6n       -1.08%

                           │ before.txt │              after.txt              │
                           │    B/op    │    B/op     vs base                 │
Entry_WithError/WithError    448.0 ± 0%   448.0 ± 0%       ~ (p=1.000 n=20) ¹
Entry_WithError/WithField    448.0 ± 0%   448.0 ± 0%       ~ (p=1.000 n=20) ¹
Entry_WithError/WithFields   448.0 ± 0%   448.0 ± 0%       ~ (p=1.000 n=20) ¹
geomean                      448.0        448.0       +0.00%
¹ all samples are equal

                           │ before.txt │              after.txt              │
                           │ allocs/op  │ allocs/op   vs base                 │
Entry_WithError/WithError    3.000 ± 0%   3.000 ± 0%       ~ (p=1.000 n=20) ¹
Entry_WithError/WithField    3.000 ± 0%   3.000 ± 0%       ~ (p=1.000 n=20) ¹
Entry_WithError/WithFields   3.000 ± 0%   3.000 ± 0%       ~ (p=1.000 n=20) ¹
geomean                      3.000        3.000       +0.00%
¹ all samples are equal

Treat values implementing error as supported fields before checking their
underlying kind.

This allows function-backed error implementations to be added through
WithError, WithField, and WithFields, matching formatter behavior that renders
error values through Error(), while continuing to reject unsupported function
values.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
The utility was skipping WithError as intermediate; wrap the logger's
WithError method instead.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR aligns WithError behavior across Entry, Logger, and package-level helpers, and updates field validation so values implementing error are accepted even when their underlying kind is a function (e.g., function-backed error implementations). This brings WithError, WithField, and WithFields in line with formatter behavior while still rejecting unsupported non-error function values.

Changes:

  • Route Entry.WithError and package-level WithError through the existing WithError/WithField paths for consistent behavior.
  • Update Entry.addField to treat error-implementing values as supported before applying function-kind rejection.
  • Simplify and strengthen tests to assert consistent acceptance/rejection and JSON formatting across WithError, WithField, and WithFields.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
exported.go Makes package-level WithError delegate to std.WithError for consistency with logger behavior.
entry.go Unifies Entry.WithError with WithField and adjusts field validation to allow function-backed error values.
entry_test.go Updates tests to validate consistent acceptance/rejection and formatting of error vs function values across APIs.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@thaJeztah
thaJeztah merged commit 134c80f into sirupsen:master Aug 18, 2026
13 checks passed
@thaJeztah
thaJeztah deleted the err_handling branch August 18, 2026 10:35
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.

2 participants