|
4 | 4 | "encoding/json"
|
5 | 5 | "encoding/xml"
|
6 | 6 | "errors"
|
| 7 | + "fmt" |
7 | 8 | "net/http"
|
8 | 9 | "reflect"
|
9 | 10 | "strconv"
|
@@ -36,11 +37,23 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
|
36 | 37 | switch {
|
37 | 38 | case strings.HasPrefix(ctype, MIMEApplicationJSON):
|
38 | 39 | 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 | + } |
40 | 47 | }
|
41 | 48 | case strings.HasPrefix(ctype, MIMEApplicationXML):
|
42 | 49 | 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 | + } |
44 | 57 | }
|
45 | 58 | case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm):
|
46 | 59 | if err = b.bindData(i, req.FormParams()); err != nil {
|
|
0 commit comments