Skip to content

Commit

Permalink
fix: Recover* report errors for any type of panic
Browse files Browse the repository at this point in the history
The previous behavior was to silently stop panics without reporting to
Sentry when the return value from recover() was not an error or a
string.

Runtime panics are specified to implement the predefined error
interface, and string is probably the second most common type passed to
panic() calls, still calls like panic(42) should not pass silently.

This fix converts any other type to string using Go's standard
formatting rules and reports the message to Sentry.
  • Loading branch information
rhcarvalho committed Jul 21, 2020
1 parent fa9ada0 commit e1bfbd4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 9 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,16 @@ func (client *Client) RecoverWithContext(
}
}

if err, ok := err.(error); ok {
event := client.eventFromException(err, LevelFatal)
return client.CaptureEvent(event, hint, scope)
var event *Event
switch err := err.(type) {
case error:
event = client.eventFromException(err, LevelFatal)
case string:
event = client.eventFromMessage(err, LevelFatal)
default:
event = client.eventFromMessage(fmt.Sprintf("%#v", err), LevelFatal)
}
if err, ok := err.(string); ok {
event := client.eventFromMessage(err, LevelFatal)
return client.CaptureEvent(event, hint, scope)
}
return nil
return client.CaptureEvent(event, hint, scope)
}

// Flush waits until the underlying Transport sends any buffered events to the
Expand Down
4 changes: 4 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ func TestRecover(t *testing.T) {
},
},
{"panic string", &Event{Message: "panic string"}},
// Arbitrary types should be converted to string:
{101010, &Event{Message: "101010"}},
{[]string{"", "", "hello"}, &Event{Message: `[]string{"", "", "hello"}`}},
{&struct{ Field string }{"test"}, &Event{Message: `&struct { Field string }{Field:"test"}`}},
}
checkEvent := func(t *testing.T, events []*Event, want *Event) {
t.Helper()
Expand Down

0 comments on commit e1bfbd4

Please sign in to comment.