Repository navigation
fix: keep encoder output valid when a value fails to encode - #10
Merged
Merged
Conversation
ssgreg
force-pushed
the
fix/encoder-errors
branch
from
December 2, 2026 12:20
4f8f3dd to
d0daf49
Compare
Contributor
Author
An Any value that encoding/json cannot marshal left its key with no value, which produced invalid JSON and broken text lines, and errors returned by Object and Array callbacks were dropped. Such a field is now replaced by a key+"Error" string field holding the error, as zap does, an Object or Array callback error is added as a key+"Error" field next to what the callback wrote, and an ArrayEncoder that calls EncodeTypeAny more than once gets separators between the elements.
ssgreg
force-pushed
the
fix/encoder-errors
branch
from
December 2, 2026 12:41
d0daf49 to
5751321
Compare
Contributor
Author
|
Force-pushed d0daf49 -> 5751321. An |
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.
Problem
Anywith a valueencoding/jsoncannot marshal writes the key and no value:The text encoder writes
ch=n=1. AnArrayEncoderthat callsEncodeTypeAnytwice gets[{"x":1}"y"], sinceEncodeTypeAnynever writes a separator. Errors returned byEncodeLogfObjectandEncodeLogfArrayare dropped.Change
Errors follow zap's convention: a string field named after the key with an
Errorsuffix.Anyfield whose value fails to marshal is replaced by that field:{"msg":"any","chError":"json: unsupported type: chan int","n":1}. The key itself is not written, so a field never holds a value of an unexpected type, which a log index with typed mappings would reject.ObjectorArraycallback is added next to whatever the callback wrote:"o":{"n":1},"oError":"boom". An inline object uses the keyError, aszap.Inlinedoes.Anyelement of an array has no key, so a failing one is written as a string with the error:[{"x":1},"y","<logf: json: unsupported type: chan int>"]. An object or array written as an element of another array has no key either, and its error is still dropped; theObjectEncoderandArrayEncoderdocs say so.EncodeTypeAnywrites a separator like every otherEncodeType*method.Not changed:
Error(nil)still writes"<nil>", andEncodestill returns anerrorthat the built-in encoders never set. Both are API questions.The two data races this PR used to fix, in the text encoder's JSON helper and in the default error encoder, moved to #16.
Testing
New tests in
encodererrors_test.gofail onmainand pass here:TestJSONEncoderEncodeErrors: unsupportedAnyas the first field, the last field and inside aGroup,Anyelements in an array, failingObject, inlineObjectandArray. Checks the exact line andjson.Valid.TestTextEncoderEncodeErrors: the same cases through the text encoder.TestEncodeErrorsAnyInBag: an unsupportedAnyin a logger Bag, encoded three times by each encoder, so the cached fragment is checked too.go test -race ./...andgolangci-lint run(v2, repo config) pass, on this branch alone and merged with #16.Benchmarks:
mainand this branch built once, ten interleaved rounds, compared withbenchstat. The machine was loaded during the run, so the spread is wide. New benchmarks inencodererrors_bench_test.goencode anObject, anArray, anAnystruct and anError. OnlyTextEncodeNestedmoved, by -2.5%, andB/opandallocs/opare unchanged in every row: