From cd82e3ff8d8ffc70f590bd656e21d585bb14b8b3 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Wed, 4 Mar 2020 17:01:54 +0100 Subject: [PATCH] Omit `internal=` in error strings --- 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) {