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 #2186 from dmuriel/bugfix-error-status
Browse files Browse the repository at this point in the history
Buffalo should respect http status when returning error
  • Loading branch information
paganotoni authored Dec 9, 2021
2 parents d179109 + 26caa52 commit bde2103
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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)
}

0 comments on commit bde2103

Please sign in to comment.