Skip to content

Commit

Permalink
Omit internal=<nil> in error strings (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishr committed Mar 5, 2020
1 parent 3e8a797 commit fc4b1c0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 14 additions & 3 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<nil>", err.Error())
}

func TestEchoClose(t *testing.T) {
Expand Down

0 comments on commit fc4b1c0

Please sign in to comment.