Skip to content

Commit 443a0bb

Browse files
committed
Merge pull request #350 from o1egl/binder_http_error_v2
Change context.Bind() return type to HTTPError. #344
2 parents 4f57798 + 981e3ad commit 443a0bb

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

context_test.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestContext(t *testing.T) {
3232
userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}"
3333
userXML := `<user><id>1</id><name>Joe</name></user>`
3434
userXMLIndent := "_<user>\n_?<id>1</id>\n_?<name>Joe</name>\n_</user>"
35+
incorrectContent := "this is incorrect content"
3536

3637
var nonMarshallableChannel chan bool
3738

@@ -66,14 +67,18 @@ func TestContext(t *testing.T) {
6667
//------
6768

6869
// JSON
69-
testBind(t, c, "application/json")
70+
testBindOk(t, c, ApplicationJSON)
71+
c.X().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
72+
testBindError(t, c, ApplicationJSON)
7073

7174
// XML
7275
c.X().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
73-
testBind(t, c, ApplicationXML)
76+
testBindOk(t, c, ApplicationXML)
77+
c.X().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
78+
testBindError(t, c, ApplicationXML)
7479

7580
// Unsupported
76-
testBind(t, c, "")
81+
testBindError(t, c, "")
7782

7883
//--------
7984
// Render
@@ -273,14 +278,30 @@ func TestContextNetContext(t *testing.T) {
273278
// assert.Equal(t, "val", c.Value("key"))
274279
}
275280

276-
func testBind(t *testing.T, c Context, ct string) {
281+
func testBindOk(t *testing.T, c Context, ct string) {
277282
c.Request().Header().Set(ContentType, ct)
278283
u := new(user)
279284
err := c.Bind(u)
280-
if ct == "" {
281-
assert.Error(t, UnsupportedMediaType)
282-
} else if assert.NoError(t, err) {
285+
if assert.NoError(t, err) {
283286
assert.Equal(t, "1", u.ID)
284287
assert.Equal(t, "Joe", u.Name)
285288
}
286289
}
290+
291+
func testBindError(t *testing.T, c Context, ct string) {
292+
c.Request().Header().Set(ContentType, ct)
293+
u := new(user)
294+
err := c.Bind(u)
295+
296+
switch ct {
297+
case ApplicationJSON, ApplicationXML:
298+
if assert.IsType(t, new(HTTPError), err) {
299+
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).code)
300+
}
301+
default:
302+
if assert.IsType(t, new(HTTPError), err) {
303+
assert.Equal(t, UnsupportedMediaType, err)
304+
}
305+
306+
}
307+
}

echo.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ var (
169169
// Errors
170170
//--------
171171

172-
UnsupportedMediaType = errors.New("unsupported media type")
172+
UnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
173173
RendererNotRegistered = errors.New("renderer not registered")
174174
InvalidRedirectCode = errors.New("invalid redirect status code")
175175

@@ -647,9 +647,13 @@ func (binder) Bind(r engine.Request, i interface{}) (err error) {
647647
ct := r.Header().Get(ContentType)
648648
err = UnsupportedMediaType
649649
if strings.HasPrefix(ct, ApplicationJSON) {
650-
err = json.NewDecoder(r.Body()).Decode(i)
650+
if err = json.NewDecoder(r.Body()).Decode(i); err != nil {
651+
err = NewHTTPError(http.StatusBadRequest, err.Error())
652+
}
651653
} else if strings.HasPrefix(ct, ApplicationXML) {
652-
err = xml.NewDecoder(r.Body()).Decode(i)
654+
if err = xml.NewDecoder(r.Body()).Decode(i); err != nil {
655+
err = NewHTTPError(http.StatusBadRequest, err.Error())
656+
}
653657
}
654658
return
655659
}

0 commit comments

Comments
 (0)