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 20, 2020
1 parent 8fcd801 commit 67627ed
Show file tree
Hide file tree
Showing 2 changed files with 15 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.Sprint(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
6 changes: 6 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ func TestRecover(t *testing.T) {
},
},
},
{
101010,
&Event{
Message: "101010",
},
},
}
checkEvent := func(t *testing.T, events []*Event, want *Event) {
t.Helper()
Expand Down

0 comments on commit 67627ed

Please sign in to comment.