mirror of
https://github.com/labstack/echo.git
synced 2024-12-28 21:08:39 +02:00
parent
e918eacd9d
commit
f83ee6cfbe
17
binder.go
17
binder.go
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@ -36,11 +37,23 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
|
||||
switch {
|
||||
case strings.HasPrefix(ctype, MIMEApplicationJSON):
|
||||
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):
|
||||
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):
|
||||
if err = b.bindData(i, req.FormParams()); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user