fix(binder): include field name in bind conversion errors (#2629)#3005
Merged
Conversation
…#2629) bindData had the field name (inputFieldName) in scope but returned bare conversion errors, so c.Bind() failures gave no indication of which field failed (e.g. "strconv.ParseInt: parsing \"10a\"..."). Wrap the conversion errors with the field name using %w so errors.Is/As still work. Matches the wrap proposed by the reporter in the issue thread. Existing tests that asserted the bare message are updated to the field-prefixed form. 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 (#2629)
When
c.Bind()fails a type conversion on form/struct data, the error gives no indication of which field failed:This makes it hard to render a useful validation message ("the
numberfield must be an integer").Fix
bindDataalready has the field name (inputFieldName) in scope at each conversion site but returned the bare error. Wrap those returns with the field name using%w(soerrors.Is/errors.Asstill work):This is the wrap the reporter proposed in the thread (
fmt.Errorf("%s: %w", inputFieldName, err)), applied to all four conversion-error sites (unmarshaler, slice element, scalar).Test
TestBind_formConversionErrorIncludesFieldName(written first; fails on master — error contains no field name). Four existing tests that asserted the bare message are updated to the field-prefixed form. gofmt/vet clean; full root-package suite passes.Fixes #2629.
🤖 Generated with Claude Code