mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-22 02:43:36 +08:00
fix(binding): include field name in BindUnmarshaler errors
Wrap UnmarshalParam / parser errors with the form tag so clients see which field failed (e.g. "uuid4: boom") without breaking %w. Fixes #4296
This commit is contained in:
parent
34dac209ff
commit
9d08bbc67e
@ -196,6 +196,14 @@ func trySetCustom(val string, value reflect.Value) (isSet bool, err error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// wrapBindErr prefixes a bind error with the form/query field name.
|
||||
func wrapBindErr(fieldName string, err error) error {
|
||||
if err == nil || fieldName == "" {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("%s: %w", fieldName, err)
|
||||
}
|
||||
|
||||
// trySetUsingParser tries to set a custom type value based on the presence of the "parser" tag on the field.
|
||||
// If the parser tag does not exist or does not match any of the supported parsers, gin will skip over this.
|
||||
func trySetUsingParser(val string, value reflect.Value, parser string) (isSet bool, err error) {
|
||||
@ -264,9 +272,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {
|
||||
return ok, err
|
||||
return ok, wrapBindErr(tagValue, err)
|
||||
} else if ok, err = trySetCustom(vs[0], value); ok {
|
||||
return ok, err
|
||||
return ok, wrapBindErr(tagValue, err)
|
||||
}
|
||||
|
||||
if vs, err = trySplit(vs, field); err != nil {
|
||||
@ -289,9 +297,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {
|
||||
return ok, err
|
||||
return ok, wrapBindErr(tagValue, err)
|
||||
} else if ok, err = trySetCustom(vs[0], value); ok {
|
||||
return ok, err
|
||||
return ok, wrapBindErr(tagValue, err)
|
||||
}
|
||||
|
||||
if vs, err = trySplit(vs, field); err != nil {
|
||||
@ -312,9 +320,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
if ok, err = trySetUsingParser(val, value, opt.parser); ok {
|
||||
return ok, err
|
||||
return ok, wrapBindErr(tagValue, err)
|
||||
} else if ok, err = trySetCustom(val, value); ok {
|
||||
return ok, err
|
||||
return ok, wrapBindErr(tagValue, err)
|
||||
}
|
||||
return true, setWithProperType(val, value, field, opt)
|
||||
}
|
||||
@ -323,9 +331,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
func setWithProperType(val string, value reflect.Value, field reflect.StructField, opt setOptions) error {
|
||||
// this if-check is required for parsing nested types like []MyId, where MyId is [12]byte
|
||||
if ok, err := trySetUsingParser(val, value, opt.parser); ok {
|
||||
return err
|
||||
return wrapBindErr(field.Name, err)
|
||||
} else if ok, err = trySetCustom(val, value); ok {
|
||||
return err
|
||||
return wrapBindErr(field.Name, err)
|
||||
}
|
||||
|
||||
// If it is a string type, no spaces are removed, and the user data is not modified here
|
||||
|
||||
@ -505,6 +505,23 @@ func (f *customUnmarshalParamHex) UnmarshalParam(param string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type failUnmarshalParam struct{}
|
||||
|
||||
func (f *failUnmarshalParam) UnmarshalParam(param string) error {
|
||||
return errors.New("boom")
|
||||
}
|
||||
|
||||
func TestBindUnmarshalerErrorIncludesFieldName(t *testing.T) {
|
||||
var s struct {
|
||||
ID failUnmarshalParam `form:"uuid4"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"uuid4": {"xxx"}}, "form")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "uuid4")
|
||||
assert.Contains(t, err.Error(), "boom")
|
||||
assert.ErrorContains(t, err, "boom") // still unwrap-friendly via %w
|
||||
}
|
||||
|
||||
func TestMappingCustomUnmarshalParamHexWithFormTag(t *testing.T) {
|
||||
var s struct {
|
||||
Foo customUnmarshalParamHex `form:"foo"`
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user