mirror of
https://github.com/labstack/echo.git
synced 2025-06-08 23:56:20 +02:00
parent
e918eacd9d
commit
f83ee6cfbe
13
binder.go
13
binder.go
@ -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,12 +37,24 @@ 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 {
|
||||||
|
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())
|
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 {
|
||||||
|
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())
|
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 {
|
||||||
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user