Skip to content

Commit 4b85e41

Browse files
author
Mohammad Alian
committed
return first if response is already committed in DefaultHTTPErrorHandler
1 parent 499097e commit 4b85e41

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

echo.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ func (e *Echo) Routers() map[string]*Router {
358358
// DefaultHTTPErrorHandler is the default HTTP error handler. It sends a JSON response
359359
// with status code.
360360
func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
361+
362+
if c.Response().Committed {
363+
return
364+
}
365+
361366
he, ok := err.(*HTTPError)
362367
if ok {
363368
if he.Internal != nil {
@@ -384,15 +389,13 @@ func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
384389
}
385390

386391
// Send response
387-
if !c.Response().Committed {
388-
if c.Request().Method == http.MethodHead { // Issue #608
389-
err = c.NoContent(he.Code)
390-
} else {
391-
err = c.JSON(code, message)
392-
}
393-
if err != nil {
394-
e.Logger.Error(err)
395-
}
392+
if c.Request().Method == http.MethodHead { // Issue #608
393+
err = c.NoContent(he.Code)
394+
} else {
395+
err = c.JSON(code, message)
396+
}
397+
if err != nil {
398+
e.Logger.Error(err)
396399
}
397400
}
398401

echo_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,15 @@ func TestDefaultHTTPErrorHandler(t *testing.T) {
11241124
"error": "stackinfo",
11251125
})
11261126
})
1127+
e.Any("/early-return", func(c Context) error {
1128+
c.String(http.StatusOK, "OK")
1129+
return errors.New("ERROR")
1130+
})
1131+
e.GET("/internal-error", func(c Context) error {
1132+
err := errors.New("internal error message body")
1133+
return NewHTTPError(http.StatusBadRequest).SetInternal(err)
1134+
})
1135+
11271136
// With Debug=true plain response contains error message
11281137
c, b := request(http.MethodGet, "/plain", e)
11291138
assert.Equal(t, http.StatusInternalServerError, c)
@@ -1136,6 +1145,17 @@ func TestDefaultHTTPErrorHandler(t *testing.T) {
11361145
c, b = request(http.MethodGet, "/servererror", e)
11371146
assert.Equal(t, http.StatusInternalServerError, c)
11381147
assert.Equal(t, "{\n \"code\": 33,\n \"error\": \"stackinfo\",\n \"message\": \"Something bad happened\"\n}\n", b)
1148+
// if the body is already set HTTPErrorHandler should not add anything to response body
1149+
c, b = request(http.MethodGet, "/early-return", e)
1150+
assert.Equal(t, http.StatusOK, c)
1151+
assert.Equal(t, "OK", b)
1152+
// internal error should be reflected in the message
1153+
c, b = request(http.MethodGet, "/internal-error", e)
1154+
assert.Equal(t, http.StatusBadRequest, c)
1155+
assert.Equal(t, `{
1156+
"error": "code=400, message=Bad Request, internal=internal error message body",
1157+
"message": "Bad Request"
1158+
}`+"\n", b)
11391159

11401160
e.Debug = false
11411161
// With Debug=false the error response is shortened

0 commit comments

Comments
 (0)