diff --git a/errors.go b/errors.go index d3b9a8820..62393356a 100644 --- a/errors.go +++ b/errors.go @@ -9,6 +9,7 @@ import ( "github.com/gobuffalo/plush" "github.com/gobuffalo/x/httpx" + "github.com/markbates/going/defaults" "github.com/pkg/errors" ) @@ -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) @@ -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{}{ diff --git a/errors_test.go b/errors_test.go index 85925c3e6..fd69ea61b 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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 {