fix: AnErr emits invalid JSON when the error is an ObjectMarshaler - #123
Open
youdie006 wants to merge 1 commit into
Open
fix: AnErr emits invalid JSON when the error is an ObjectMarshaler#123youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
When an error passed to .Err()/.AnErr() also implements ObjectMarshaler, the
logger emitted invalid JSON. AnErr wrote the "error": key then called
o.MarshalObject(e) directly; each field method prepends a comma, so the buffer
became "error":,"code":500,... with a stray leading comma and the object's
fields leaking to the top level (json.Valid false).
Mirror the existing Object() convention: record n := len(e.buf) before
MarshalObject, then rewrite the stray leading comma at n to '{' and append '}'
(an empty marshaler yields null). The non-ObjectMarshaler string path is
unchanged.
Fixes phuslu#110
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.
Fixes #110.
Problem
When an
errorpassed to.Err(err)/.AnErr(key, err)also implementsObjectMarshaler, the logger emits invalid JSON.AnErrwrites the"error":key and then callso.MarshalObject(e)directly. Each field method inMarshalObjectprepends a comma (e.g.,"code":500), so the buffer ends up as:The leading comma is stray and the object's fields leak to the top level;
json.Valid()returns false.Root cause
AnErrdoes not wrap the marshaler's output in braces or fix the leading comma. The library's own reference implementationObject()already does this correctly: it recordsn := len(e.buf)before callingMarshalObject, then rewrites the first byte (the stray comma) to{and appends}.Fix
Mirror the existing
Object()convention insideAnErr'sObjectMarshalerbranch (~7 lines). The non-ObjectMarshalerstring path is unchanged, and an empty marshaler now yieldsnull(same asObject()).Tests
Added
TestAnErrObjectMarshaler: an error type implementing botherrorandObjectMarshaler, logged via.Err(err)to a buffer, assertingjson.Valid(output) == trueand that the object is nested as"error":{"code":500,"message":"boom"}(not"error":,).Red-green verified in docker
golang:latest:"error":,"code":500,...,json.Validfalse.Existing object/error suite (
TestLoggerObject,TestLoggerObjects,TestLoggerErrorStack,TestFixMissingErrEntry) still passes;gofmtclean.Note (follow-up, not in this PR)
Any()has the same latent bug in itscase ObjectMarshaler:branch (logger.go ~L2319) -- it also appends"key":then callsMarshalObjectdirectly. Worth a separate fix.AI-assisted: this change was prepared with the help of an AI coding assistant and reviewed by me.