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 all 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
12 changes: 5 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ func (a *App) defaultErrorMiddleware(next Handler) Handler {
}
status := http.StatusInternalServerError
// unpack root err and check for HTTPError
switch {
case errors.Is(err, sql.ErrNoRows):
if errors.Is(err, sql.ErrNoRows) {
status = http.StatusNotFound
default:
var h HTTPError
if errors.As(err, &h) {
status = h.Status
}
}
var h HTTPError
if errors.As(err, &h) {
status = h.Status
}
payload := events.Payload{
"context": c,
Expand Down
19 changes: 19 additions & 0 deletions route_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package buffalo

import (
"database/sql"
"fmt"
"net/http"
"testing"

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

app.GET("/bad-2", func(c Context) error {
return sql.ErrTxDone
})

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

app.GET("/gone-wrap", func(c Context) error {
return c.Error(http.StatusGone, fmt.Errorf("some error wrapping here: %w", sql.ErrNoRows))
})

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)
}