1
0
mirror of https://github.com/labstack/echo.git synced 2025-06-06 23:46:16 +02:00

Enhanced default http error handler

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2019-08-01 22:20:33 -07:00
parent 87da9a948b
commit ed51400a81
2 changed files with 16 additions and 20 deletions

34
echo.go
View File

@ -99,9 +99,9 @@ type (
// HTTPError represents an error that occurred while handling a request. // HTTPError represents an error that occurred while handling a request.
HTTPError struct { HTTPError struct {
Code int Code int `json:"code"`
Message interface{} Message interface{} `json:"message"`
Internal error // Stores the error returned by an external dependency Internal error `json:"-"` // Stores the error returned by an external dependency
} }
// MiddlewareFunc defines a function to process middleware. // MiddlewareFunc defines a function to process middleware.
@ -341,32 +341,28 @@ func (e *Echo) Routers() map[string]*Router {
// DefaultHTTPErrorHandler is the default HTTP error handler. It sends a JSON response // DefaultHTTPErrorHandler is the default HTTP error handler. It sends a JSON response
// with status code. // with status code.
func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) { func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
var ( he, ok := err.(*HTTPError)
code = http.StatusInternalServerError if ok {
msg interface{}
)
if he, ok := err.(*HTTPError); ok {
code = he.Code
msg = he.Message
if he.Internal != nil { if he.Internal != nil {
err = fmt.Errorf("%v, %v", err, he.Internal) err = fmt.Errorf("%v, %v", err, he.Internal)
} }
} else if e.Debug {
msg = err.Error()
} else { } else {
msg = http.StatusText(code) he = &HTTPError{
Code: http.StatusInternalServerError,
}
} }
if _, ok := msg.(string); ok { if e.Debug {
msg = Map{"message": msg} he.Message = err.Error()
} else {
he.Message = http.StatusText(he.Code)
} }
// Send response // Send response
if !c.Response().Committed { if !c.Response().Committed {
if c.Request().Method == http.MethodHead { // Issue #608 if c.Request().Method == http.MethodHead { // Issue #608
err = c.NoContent(code) err = c.NoContent(he.Code)
} else { } else {
err = c.JSON(code, msg) err = c.JSON(he.Code, he)
} }
if err != nil { if err != nil {
e.Logger.Error(err) e.Logger.Error(err)
@ -749,7 +745,7 @@ func NewHTTPError(code int, message ...interface{}) *HTTPError {
// Error makes it compatible with `error` interface. // Error makes it compatible with `error` interface.
func (he *HTTPError) Error() string { func (he *HTTPError) Error() string {
return fmt.Sprintf("code=%d, message=%v", he.Code, he.Message) return fmt.Sprintf("code=%d, message=%v, internal=%v", he.Code, he.Message, he.Internal)
} }
// SetInternal sets error to HTTPError.Internal // SetInternal sets error to HTTPError.Internal

View File

@ -534,7 +534,7 @@ func TestHTTPError(t *testing.T) {
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{ err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
"code": 12, "code": 12,
}) })
assert.Equal(t, "code=400, message=map[code:12]", err.Error()) assert.Equal(t, "code=400, message=map[code:12], internal=<nil>", err.Error())
} }
func TestEchoClose(t *testing.T) { func TestEchoClose(t *testing.T) {