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

make sure all caught panics have a stack with them #1176

Merged
merged 4 commits into from
Jul 16, 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
5 changes: 3 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ func (a *App) PanicHandler(next Handler) Handler {
if r != nil { //catch
switch t := r.(type) {
case error:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might have to do err = t otherwise err is nill still

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

err = errors.WithStack(t)
err = t
case string:
err = errors.WithStack(errors.New(t))
err = errors.New(t)
default:
err = errors.New(fmt.Sprint(t))
}
err = errors.WithStack(err)
eh := a.ErrorHandlers.Get(500)
eh(500, err, c)
}
Expand Down
43 changes: 43 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package buffalo

import (
"testing"

"github.com/markbates/willie"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

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

table := []struct {
path string
expected string
}{
{"/string", "string boom"},
{"/error", "error boom"},
}

const stack = `github.com/gobuffalo/buffalo.(*App).PanicHandler`

w := willie.New(app)
for _, tt := range table {
t.Run(tt.path, func(st *testing.T) {
r := require.New(st)

res := w.HTML(tt.path).Get()
r.Equal(500, res.Code)

body := res.Body.String()
r.Contains(body, tt.expected)
r.Contains(body, stack)
})
}
}