Render errors cleanly for both CLIs and structured logs#28
Merged
Conversation
ezekiel-alexrod
approved these changes
Jun 4, 2026
ef78556 to
75d3134
Compare
Error() (and the %s/%q verbs) is the message form: it feeds CLI output, %w chains and any logger that prints err.Error(). Baking the stack into it left no way to get the plain message and produced noisy "... at=[(func='...', file='...', line='...')]" tails wherever an error was shown to a human. Error() now carries only title, identifier, details, properties and cause. The full message-plus-stack dump stays available through the %+v verb (unchanged). The "(identifier)" segment is also omitted when no identifier is set, so untyped errors read "title: detail" instead of "title (): detail". Note: this changes the string returned by Error(); callers that parsed it for the stack must switch to %+v.
slog's handlers serialise an error value differently: the JSON handler
falls back to err.Error() (a string) while the text handler uses %+v
(JSON). Combined with go-errors' formatting that inverts the output —
text logs show JSON, JSON logs show the bare message.
Implement slog.LogValuer on *Error so both handlers resolve the same
structured group: title, identifier, details, properties, cause and the
stack. JSON consumers get queryable fields (the stack as an array of
{function,file,line} objects); the text handler renders each frame via
the new Trace.String(), so the stack reads as "<func> <file>:<line>"
instead of pointer addresses.
The stack is part of the log form (post-mortem debugging) but stays out
of Error() (the human/CLI form): the two audiences are served on purpose.
75d3134 to
4ac88af
Compare
anthony-treuillier-scality
approved these changes
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Error() baked the stack trace into its output, so every human-facing error (CLI, %w chains, err.Error() logs) carried a noisy at=[(func=..., file=..., line=...)] tail — with no way to get the plain message. And under slog the two handlers disagreed: the JSON handler falls back to Error() while the text handler uses %+v, so text logs printed JSON and JSON logs printed the bare string.
This PR splits the two audiences cleanly: