fix(binder): serialize BindingError to structured JSON (#2771)#3004
Merged
Conversation
BindingError embeds *HTTPError but did not implement json.Marshaler, so
DefaultHTTPErrorHandler's type switch fell through to its default branch
(the value is a *BindingError, not a *HTTPError), flattening responses to
{"message":"Bad Request"} and dropping both the field name and the binder
message — a regression from fbfe216.
Implement MarshalJSON on *BindingError so it takes the handler's
json.Marshaler branch, restoring the structured {"field":...,"message":...}
response (the v4.10.2 behavior).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…llback (#2771) - Note on the struct that serialization goes through MarshalJSON (the json tags are documentation only now). - Add a test for the empty-message -> status-text fallback branch. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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 (#2771)
A binding error returned from a handler is serialized as
{"message":"Bad Request"}— the field name and the binder message are both lost.Root cause
BindingErrorembeds*HTTPErrorbut does not implementjson.Marshaler. InDefaultHTTPErrorHandlerthe type switch runs on theHTTPStatusCoderextracted viaerrors.As, whose dynamic type is*BindingError. Go'scase *HTTPErrormatches only the exact type, so a*BindingErrorfalls through to thedefaultbranch →{"message": http.StatusText(code)}. Regression from fbfe216 (#2456).Verified on current
master:Fix
Implement
MarshalJSONon*BindingErrorso it takes the handler's existingcase json.Marshalerbranch (which is checked before*HTTPError). Restores the v4.10.2 structured response:This is the approach the maintainer outlined in the issue thread ("we could make
echo.BindingError… implementjson.Marshaler").Test
TestBindingError_serializesToStructuredJSON(written first; fails on master withfield=<nil>,message="Bad Request"). gofmt/vet clean; full root-package suite passes.Fixes #2771.
🤖 Generated with Claude Code