mirror of
https://github.com/labstack/echo.git
synced 2025-11-29 22:48:07 +02:00
Add field name in Error message when Binding type mismatch
Old error message ` Unmarshal type error: expected=int, got=string, offset=47 ` New error message ` Unmarshal type error: expected=int, got=string, field=age, offset=47 ` Make it easy to fix for client.
This commit is contained in:
2
bind.go
2
bind.go
@@ -44,7 +44,7 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
||||
case strings.HasPrefix(ctype, MIMEApplicationJSON):
|
||||
if err = json.NewDecoder(req.Body).Decode(i); err != nil {
|
||||
if ute, ok := err.(*json.UnmarshalTypeError); ok {
|
||||
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, offset=%v", ute.Type, ute.Value, ute.Offset))
|
||||
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset))
|
||||
} else if se, ok := err.(*json.SyntaxError); ok {
|
||||
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error()))
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user