From f53c9246ca5dac53f8a3d03c0ccc38984531d3a7 Mon Sep 17 00:00:00 2001 From: Stefan Aurori Date: Fri, 15 Apr 2022 12:04:46 -0400 Subject: [PATCH 1/2] HTTPError Error() avoid nil pointer dereference on nil Cause --- errors.go | 2 +- errors_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index f98595419..fc764861e 100644 --- a/errors.go +++ b/errors.go @@ -30,7 +30,7 @@ func (h HTTPError) Unwrap() error { // Error returns the cause of the error as string. func (h HTTPError) Error() string { - return h.Cause.Error() + return fmt.Sprint(h.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 { From 1acf05b3e1af3a2e28bd5a8bbba93127ed0f9a8e Mon Sep 17 00:00:00 2001 From: Stefan Aurori Date: Sat, 16 Apr 2022 11:00:02 -0400 Subject: [PATCH 2/2] HTTPError Error() return when Cause is nil --- errors.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index fc764861e..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 fmt.Sprint(h.Cause) + if h.Cause != nil { + return h.Cause.Error() + } + return "unknown cause" } // ErrorHandler interface for handling an error for a