Skip to content

Omit internal=<nil> in error strings #1525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Omit internal=<nil> in error strings
  • Loading branch information
flimzy committed Mar 4, 2020
commit cd82e3ff8d8ffc70f590bd656e21d585bb14b8b3
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