Skip to content

Commit

Permalink
allow to custom the error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
sidbusy committed Jul 13, 2016
1 parent f931d1e commit d1d5747
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 11 deletions.
6 changes: 1 addition & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,7 @@ func (c *Context) BindJSON(obj interface{}) error {
// BindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func (c *Context) BindWith(obj interface{}, b binding.Binding) error {
if err := b.Bind(c.Request, obj); err != nil {
c.AbortWithError(400, err).SetType(ErrorTypeBind)
return err
}
return nil
return b.Bind(c.Request, obj)
}

// ClientIP implements a best effort algorithm to return the real client IP, it parses
Expand Down
7 changes: 1 addition & 6 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,22 +671,17 @@ func TestContextBindWithJSON(t *testing.T) {
}

func TestContextBadAutoBind(t *testing.T) {
c, w, _ := CreateTestContext()
c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "http://example.com", bytes.NewBufferString("\"foo\":\"bar\", \"bar\":\"foo\"}"))
c.Request.Header.Add("Content-Type", MIMEJSON)
var obj struct {
Foo string `json:"foo"`
Bar string `json:"bar"`
}

assert.False(t, c.IsAborted())
assert.Error(t, c.Bind(&obj))
c.Writer.WriteHeaderNow()

assert.Empty(t, obj.Bar)
assert.Empty(t, obj.Foo)
assert.Equal(t, w.Code, 400)
assert.True(t, c.IsAborted())
}

func TestContextGolangContext(t *testing.T) {
Expand Down

0 comments on commit d1d5747

Please sign in to comment.