1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +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 struct {
Code int
Message interface{}
Internal error // Stores the error returned by an external dependency
Code int `json:"code"`
Message interface{} `json:"message"`
Internal error `json:"-"` // Stores the error returned by an external dependency
}
// 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
// with status code.
func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
var (
code = http.StatusInternalServerError
msg interface{}
)
if he, ok := err.(*HTTPError); ok {
code = he.Code
msg = he.Message
he, ok := err.(*HTTPError)
if ok {
if he.Internal != nil {
err = fmt.Errorf("%v, %v", err, he.Internal)
}
} else if e.Debug {
msg = err.Error()
} else {
msg = http.StatusText(code)
he = &HTTPError{
Code: http.StatusInternalServerError,
}
}
if _, ok := msg.(string); ok {
msg = Map{"message": msg}
if e.Debug {
he.Message = err.Error()
} else {
he.Message = http.StatusText(he.Code)
}
// Send response
if !c.Response().Committed {
if c.Request().Method == http.MethodHead { // Issue #608
err = c.NoContent(code)
err = c.NoContent(he.Code)
} else {
err = c.JSON(code, msg)
err = c.JSON(he.Code, he)
}
if err != nil {
e.Logger.Error(err)
@ -749,7 +745,7 @@ func NewHTTPError(code int, message ...interface{}) *HTTPError {
// Error makes it compatible with `error` interface.
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

View File

@ -534,7 +534,7 @@ func TestHTTPError(t *testing.T) {
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
"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) {