diff --git a/echo.go b/echo.go index fa1c93ec7..86e7b2aae 100644 --- a/echo.go +++ b/echo.go @@ -783,6 +783,9 @@ func NewHTTPError(code int, message ...interface{}) *HTTPError { // Error makes it compatible with `error` interface. func (he *HTTPError) Error() string { + if he.Internal == nil { + 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) } diff --git a/echo_test.go b/echo_test.go index 68c556f41..ddbc56f27 100644 --- a/echo_test.go +++ b/echo_test.go @@ -543,10 +543,21 @@ func request(method, path string, e *Echo) (int, string) { } func TestHTTPError(t *testing.T) { - err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{ - "code": 12, + t.Run("non-internal", func(t *testing.T) { + err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{ + "code": 12, + }) + + assert.Equal(t, "code=400, message=map[code:12]", err.Error()) + + }) + t.Run("internal", func(t *testing.T) { + err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{ + "code": 12, + }) + err.SetInternal(errors.New("internal error")) + assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error()) }) - assert.Equal(t, "code=400, message=map[code:12], internal=", err.Error()) } func TestEchoClose(t *testing.T) {