feat: make a more detailed error message during field value setting failed

This commit is contained in:
iseki 2025-07-22 17:01:56 +08:00
parent 9af6d510b5
commit d3b49c3d23
No known key found for this signature in database
GPG Key ID: B5F8E8F1CE406872

View File

@ -944,6 +944,22 @@ func (field *Field) setupValuerAndSetter() {
}
}
}
var oldSetter = field.Set
field.Set = func(ctx context.Context, value reflect.Value, v interface{}) (err error) {
err = oldSetter(ctx, value, v)
if err == nil {
return
}
var errMessage = err.Error()
// It's ugly and unrobust, but it's required since the fallbackSetter create error by fmt.Errorf directly and here are some nested setter calls.
// Without string manipulation, there's no way to avoid a duplicated error message.
// Also, adding many fmt.Errorf calls in all setter implementations is not a good idea, apparently.
if strings.Contains(errMessage, "to field "+field.Name) {
return
}
err = fmt.Errorf("failed to set value to field %s: %w", field.Name, err)
return
}
if field.Serializer != nil {
var (