From fc4b1c0a830f194bf6334d2b3e61d92cccebb2be Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 5 Mar 2020 06:36:43 -0800 Subject: [PATCH] Omit `internal=` in error strings (#1525) --- echo.go | 3 +++ echo_test.go | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) 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) {