Skip to content

Commit fc4b1c0

Browse files
authored
Omit internal=<nil> in error strings (#1525)
1 parent 3e8a797 commit fc4b1c0

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

echo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,9 @@ func NewHTTPError(code int, message ...interface{}) *HTTPError {
783783

784784
// Error makes it compatible with `error` interface.
785785
func (he *HTTPError) Error() string {
786+
if he.Internal == nil {
787+
return fmt.Sprintf("code=%d, message=%v", he.Code, he.Message)
788+
}
786789
return fmt.Sprintf("code=%d, message=%v, internal=%v", he.Code, he.Message, he.Internal)
787790
}
788791

echo_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,21 @@ func request(method, path string, e *Echo) (int, string) {
543543
}
544544

545545
func TestHTTPError(t *testing.T) {
546-
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
547-
"code": 12,
546+
t.Run("non-internal", func(t *testing.T) {
547+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
548+
"code": 12,
549+
})
550+
551+
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
552+
553+
})
554+
t.Run("internal", func(t *testing.T) {
555+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
556+
"code": 12,
557+
})
558+
err.SetInternal(errors.New("internal error"))
559+
assert.Equal(t, "code=400, message=map[code:12], internal=internal error", err.Error())
548560
})
549-
assert.Equal(t, "code=400, message=map[code:12], internal=<nil>", err.Error())
550561
}
551562

552563
func TestEchoClose(t *testing.T) {

0 commit comments

Comments
 (0)