Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover and RecoverWithContext with arbitrary types #219

Closed
rhcarvalho opened this issue May 5, 2020 · 1 comment · Fixed by #268
Closed

Recover and RecoverWithContext with arbitrary types #219

rhcarvalho opened this issue May 5, 2020 · 1 comment · Fixed by #268

Comments

@rhcarvalho
Copy link
Contributor

Recover and RecoverWithContext send events to Sentry when they recover from panics and the recovered value has type error or string. Any other types are ignored.

sentry-go/client.go

Lines 233 to 284 in 550a179

// Recover captures a panic.
// Returns `EventID` if successfully, or `nil` if there's no error to recover from.
func (client *Client) Recover(err interface{}, hint *EventHint, scope EventModifier) *EventID {
if err == nil {
err = recover()
}
if err != nil {
if err, ok := err.(error); ok {
event := client.eventFromException(err, LevelFatal)
return client.CaptureEvent(event, hint, scope)
}
if err, ok := err.(string); ok {
event := client.eventFromMessage(err, LevelFatal)
return client.CaptureEvent(event, hint, scope)
}
}
return nil
}
// Recover captures a panic and passes relevant context object.
// Returns `EventID` if successfully, or `nil` if there's no error to recover from.
func (client *Client) RecoverWithContext(
ctx context.Context,
err interface{},
hint *EventHint,
scope EventModifier,
) *EventID {
if err == nil {
err = recover()
}
if err != nil {
if hint.Context == nil && ctx != nil {
hint.Context = ctx
}
if err, ok := err.(error); ok {
event := client.eventFromException(err, LevelFatal)
return client.CaptureEvent(event, hint, scope)
}
if err, ok := err.(string); ok {
event := client.eventFromMessage(err, LevelFatal)
return client.CaptureEvent(event, hint, scope)
}
}
return nil
}

We should send events no matter the type we're recovering (and need to handle the case in which the value is not serializable as JSON).

@trungdq88
Copy link

I'm glad that this is reported, I wasted 1 hour debugging why my panic(123) didn't work :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants