Skip to content

Commit 08240a3

Browse files
vishrclaude
andcommitted
docs/test(binder): clarify BindingError JSON + cover empty-message fallback (#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>
1 parent 42410d1 commit 08240a3

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎binder.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ import (
6666
*/
6767

6868
// BindingError represents an error that occurred while binding request data.
69+
//
70+
// Note: JSON serialization is handled by the MarshalJSON method below, not by the
71+
// struct tags (which are kept for documentation). MarshalJSON emits {"field","message"}.
6972
type BindingError struct {
7073
// Field is the field name where value binding failed
7174
Field string `json:"field"`

‎binder_error_response_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ func TestBindingError_serializesToStructuredJSON(t *testing.T) {
3333
assert.Equal(t, "docNum", body["field"], "binding error response must retain the field name")
3434
assert.Equal(t, "failed to bind field value to int", body["message"], "binding error response must retain the binder message")
3535
}
36+
37+
// When the binding error carries no message, MarshalJSON falls back to the
38+
// status text (mirroring DefaultHTTPErrorHandler's *HTTPError branch).
39+
func TestBindingError_marshalJSON_emptyMessageFallsBackToStatusText(t *testing.T) {
40+
be := &BindingError{Field: "name", HTTPError: &HTTPError{Code: http.StatusBadRequest}}
41+
42+
b, err := be.MarshalJSON()
43+
44+
assert.NoError(t, err)
45+
assert.JSONEq(t, `{"field":"name","message":"Bad Request"}`, string(b))
46+
}

0 commit comments

Comments
 (0)