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

Buffalo should respect http status when returning error #2186

Merged
merged 5 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (a *App) defaultErrorMiddleware(next Handler) Handler {
switch {
case errors.Is(err, sql.ErrNoRows):
status = http.StatusNotFound
fallthrough
default:
var h HTTPError
if errors.As(err, &h) {
Expand Down
15 changes: 15 additions & 0 deletions route_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/httptest"
"github.com/pkg/errors"
paganotoni marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/require"
)

Expand All @@ -22,11 +23,25 @@ func Test_RouteInfo_ServeHTTP_SQL_Error(t *testing.T) {
return sql.ErrNoRows
})

app.GET("/gone-unwrap", func(c Context) error {
return c.Error(http.StatusGone, sql.ErrNoRows)
})

app.GET("/gone-wrap", func(c Context) error {
return c.Error(http.StatusGone, errors.Wrap(sql.ErrNoRows, "some error wrapping here"))
})

w := httptest.New(app)

res := w.HTML("/good").Get()
r.Equal(http.StatusOK, res.Code)

res = w.HTML("/bad").Get()
r.Equal(http.StatusNotFound, res.Code)

res = w.HTML("/gone-wrap").Get()
r.Equal(http.StatusGone, res.Code)

res = w.HTML("/gone-unwrap").Get()
r.Equal(http.StatusGone, res.Code)
}