Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2238 from saurori/nil-error
Browse files Browse the repository at this point in the history
HTTPError Error() avoid nil pointer dereference on nil Cause
  • Loading branch information
paganotoni authored Apr 16, 2022
2 parents 6531d1c + 1acf05b commit be59b08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ func Test_defaultErrorHandler_XML_production(t *testing.T) {
r.Contains(b, `</response>`)
}

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 {
Expand Down

0 comments on commit be59b08

Please sign in to comment.