forked from Fuyu/router
Add error to return of Reader
This commit is contained in:
parent
4c83818ecd
commit
a16ee2740c
@ -17,12 +17,11 @@ func defaultErrorHandler(c *Context, err interface{}) {
|
|||||||
_ = c.StatusText(http.StatusInternalServerError)
|
_ = c.StatusText(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultReader(c *Context, dst interface{}) bool {
|
func defaultReader(c *Context, dst interface{}) (bool, error) {
|
||||||
err := json.NewDecoder(c.Request.Body).Decode(dst)
|
err := json.NewDecoder(c.Request.Body).Decode(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.NoContent(400)
|
return false, c.StatusText(http.StatusBadRequest)
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true, nil
|
||||||
}
|
}
|
||||||
|
13
router.go
13
router.go
@ -24,7 +24,7 @@ type ErrorHandle func(*Context, interface{})
|
|||||||
type Middleware func(Handle) Handle
|
type Middleware func(Handle) Handle
|
||||||
|
|
||||||
// Binder reads input to dst, returns true is successful
|
// Binder reads input to dst, returns true is successful
|
||||||
type Reader func(c *Context, dst interface{}) bool
|
type Reader func(c *Context, dst interface{}) (bool, error)
|
||||||
|
|
||||||
// Router is the router itself
|
// Router is the router itself
|
||||||
type Router struct {
|
type Router struct {
|
||||||
@ -164,11 +164,16 @@ func handlePOST(r *Router, f interface{}) Handle {
|
|||||||
return func(c *Context) error {
|
return func(c *Context) error {
|
||||||
data := reflect.New(inputRt)
|
data := reflect.New(inputRt)
|
||||||
|
|
||||||
if !r.Reader(c, data.Interface()) {
|
if r.Reader != nil {
|
||||||
|
ok, err := r.Reader(c, data.Interface())
|
||||||
c.Request.Body.Close()
|
c.Request.Body.Close()
|
||||||
return nil
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.Request.Body.Close()
|
|
||||||
|
|
||||||
out := funcRv.Call([]reflect.Value{reflect.ValueOf(c), data.Elem()})
|
out := funcRv.Call([]reflect.Value{reflect.ValueOf(c), data.Elem()})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user