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

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into rename-upgrade
Browse files Browse the repository at this point in the history
stanislas-m authored Jul 24, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 892ebd8 + 6a45b47 commit a5eff5b
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -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{}{
14 changes: 14 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -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 {

0 comments on commit a5eff5b

Please sign in to comment.