Skip to content

Commit f83ee6c

Browse files
denyskochvishr
authored andcommitted
Closes #610, Closes #611
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent e918eac commit f83ee6c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

binder.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"encoding/xml"
66
"errors"
7+
"fmt"
78
"net/http"
89
"reflect"
910
"strconv"
@@ -36,11 +37,23 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
3637
switch {
3738
case strings.HasPrefix(ctype, MIMEApplicationJSON):
3839
if err = json.NewDecoder(req.Body()).Decode(i); err != nil {
39-
err = NewHTTPError(http.StatusBadRequest, err.Error())
40+
if ute, ok := err.(*json.UnmarshalTypeError); ok {
41+
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unmarshal type error: expected=%v, got=%v, offset=%v", ute.Type, ute.Value, ute.Offset))
42+
} else if se, ok := err.(*json.SyntaxError); ok {
43+
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: offset=%v, error=%v", se.Offset, se.Error()))
44+
} else {
45+
err = NewHTTPError(http.StatusBadRequest, err.Error())
46+
}
4047
}
4148
case strings.HasPrefix(ctype, MIMEApplicationXML):
4249
if err = xml.NewDecoder(req.Body()).Decode(i); err != nil {
43-
err = NewHTTPError(http.StatusBadRequest, err.Error())
50+
if ute, ok := err.(*xml.UnsupportedTypeError); ok {
51+
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unsupported type error: type=%v, error=%v", ute.Type, ute.Error()))
52+
} else if se, ok := err.(*xml.SyntaxError); ok {
53+
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: line=%v, error=%v", se.Line, se.Error()))
54+
} else {
55+
err = NewHTTPError(http.StatusBadRequest, err.Error())
56+
}
4457
}
4558
case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm):
4659
if err = b.bindData(i, req.FormParams()); err != nil {

0 commit comments

Comments
 (0)