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

Default error handler content type fixes #1150 #1195

Merged
merged 1 commit into from
Jul 24, 2018
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
4 changes: 3 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gobuffalo/plush"
"github.com/gobuffalo/x/httpx"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -83,6 +84,8 @@ func productionErrorResponseFor(status int) []byte {

func defaultErrorHandler(status int, origErr error, c Context) error {
env := c.Value("env")
ct := defaults.String(httpx.ContentType(c.Request()), "text/html")
c.Response().Header().Set("content-type", ct)

c.Logger().Error(origErr)
c.Response().WriteHeader(status)
Expand All @@ -94,7 +97,6 @@ func defaultErrorHandler(status int, origErr error, c Context) error {
}

msg := fmt.Sprintf("%+v", origErr)
ct := httpx.ContentType(c.Request())
switch strings.ToLower(ct) {
case "application/json", "text/json", "json":
err := json.NewEncoder(c.Response()).Encode(map[string]interface{}{
Expand Down
14 changes: 14 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import (
"github.com/stretchr/testify/require"
)

func Test_defaultErrorHandler_SetsContentType(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(401, errors.New("boom"))
})

w := willie.New(app)
res := w.HTML("/").Get()
r.Equal(401, res.Code)
ct := res.Header().Get("content-type")
r.Equal("text/html", ct)
}

func Test_PanicHandler(t *testing.T) {
app := New(Options{})
app.GET("/string", func(c Context) error {
Expand Down