diff --git a/errors.go b/errors.go index f98595419..228bd3865 100644 --- a/errors.go +++ b/errors.go @@ -30,7 +30,10 @@ func (h HTTPError) Unwrap() error { // Error returns the cause of the error as string. func (h HTTPError) Error() string { - return h.Cause.Error() + if h.Cause != nil { + return h.Cause.Error() + } + return "unknown cause" } // ErrorHandler interface for handling an error for a diff --git a/errors_test.go b/errors_test.go index 7042ade25..bf55f6002 100644 --- a/errors_test.go +++ b/errors_test.go @@ -143,6 +143,18 @@ func Test_defaultErrorHandler_XML_production(t *testing.T) { r.Contains(b, ``) } +func Test_defaultErrorHandler_nil_error(t *testing.T) { + r := require.New(t) + app := New(Options{}) + app.GET("/", func(c Context) error { + return c.Error(http.StatusInternalServerError, nil) + }) + + w := httptest.New(app) + res := w.JSON("/").Get() + r.Equal(http.StatusInternalServerError, res.Code) +} + func Test_PanicHandler(t *testing.T) { app := New(Options{}) app.GET("/string", func(c Context) error {