1
0
mirror of https://github.com/labstack/echo.git synced 2025-06-08 23:56:20 +02:00

Closes #610, Closes #611

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Denys Koch 2016-08-01 22:27:00 +02:00 committed by Vishal Rana
parent e918eacd9d
commit f83ee6cfbe

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt"
"net/http" "net/http"
"reflect" "reflect"
"strconv" "strconv"
@ -36,11 +37,23 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
switch { switch {
case strings.HasPrefix(ctype, MIMEApplicationJSON): case strings.HasPrefix(ctype, MIMEApplicationJSON):
if err = json.NewDecoder(req.Body()).Decode(i); err != nil { if err = json.NewDecoder(req.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error()) if ute, ok := err.(*json.UnmarshalTypeError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unmarshal type error: expected=%v, got=%v, offset=%v", ute.Type, ute.Value, ute.Offset))
} else if se, ok := err.(*json.SyntaxError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: offset=%v, error=%v", se.Offset, se.Error()))
} else {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
} }
case strings.HasPrefix(ctype, MIMEApplicationXML): case strings.HasPrefix(ctype, MIMEApplicationXML):
if err = xml.NewDecoder(req.Body()).Decode(i); err != nil { if err = xml.NewDecoder(req.Body()).Decode(i); err != nil {
err = NewHTTPError(http.StatusBadRequest, err.Error()) if ute, ok := err.(*xml.UnsupportedTypeError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("unsupported type error: type=%v, error=%v", ute.Type, ute.Error()))
} else if se, ok := err.(*xml.SyntaxError); ok {
err = NewHTTPError(http.StatusBadRequest, fmt.Sprintf("syntax error: line=%v, error=%v", se.Line, se.Error()))
} else {
err = NewHTTPError(http.StatusBadRequest, err.Error())
}
} }
case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm): case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm):
if err = b.bindData(i, req.FormParams()); err != nil { if err = b.bindData(i, req.FormParams()); err != nil {