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:
17
bind_test.go
17
bind_test.go
@@ -208,6 +208,23 @@ func TestBindbindData(t *testing.T) {
|
||||
assertBindTestStruct(t, ts)
|
||||
}
|
||||
|
||||
func TestBindUnmarshalTypeError(t *testing.T) {
|
||||
body := bytes.NewBufferString(`{ "id": "text" }`)
|
||||
e := New()
|
||||
req := httptest.NewRequest(POST, "/", body)
|
||||
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
u := new(user)
|
||||
|
||||
err := c.Bind(u)
|
||||
|
||||
he := &HTTPError{Code: http.StatusBadRequest, Message: "Unmarshal type error: expected=int, got=string, field=id, offset=14"}
|
||||
|
||||
assert.Equal(t, he, err)
|
||||
}
|
||||
|
||||
func TestBindSetWithProperType(t *testing.T) {
|
||||
ts := new(bindTestStruct)
|
||||
typ := reflect.TypeOf(ts).Elem()
|
||||
|
||||
Reference in New Issue
Block a user