Skip to content

Commit

Permalink
fix: Ignore err.Cause() when it is nil (#160)
Browse files Browse the repository at this point in the history
Fixes a condition that would lead to a panic.
  • Loading branch information
mikekonan authored Feb 4, 2020
1 parent 06802cf commit 1cd7967
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ func (client *Client) eventFromException(exception error, level Level) *Event {
cause := exception
// Handle wrapped errors for github.com/pingcap/errors and github.com/pkg/errors
if ex, ok := exception.(interface{ Cause() error }); ok {
cause = ex.Cause()
if c := ex.Cause(); c != nil {
cause = c
}
}

event := NewEvent()
Expand Down
26 changes: 26 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ func TestCaptureExceptionShouldExtractCorrectTypeAndValueForWrappedErrors(t *tes
assertEqual(t, transport.lastEvent.Exception[0].Value, "wat")
}

type customErrWithCause struct{ cause error }

func (e *customErrWithCause) Error() string {
return "err"
}

func (e *customErrWithCause) Cause() error {
return e.cause
}

func TestCaptureExceptionShouldNotUseCauseIfCauseIsNil(t *testing.T) {
client, scope, transport := setupClientTest()
err := &customErrWithCause{cause: nil}
client.CaptureException(err, nil, scope)
assertEqual(t, transport.lastEvent.Exception[0].Type, "*sentry.customErrWithCause")
assertEqual(t, transport.lastEvent.Exception[0].Value, "err")
}

func TestCaptureExceptionShouldUseCauseIfCauseIsNotNil(t *testing.T) {
client, scope, transport := setupClientTest()
err := &customErrWithCause{cause: &customErr{}}
client.CaptureException(err, nil, scope)
assertEqual(t, transport.lastEvent.Exception[0].Type, "*sentry.customErr")
assertEqual(t, transport.lastEvent.Exception[0].Value, "wat")
}

func TestCaptureEventShouldSendEventWithProvidedError(t *testing.T) {
client, scope, transport := setupClientTest()
event := NewEvent()
Expand Down

0 comments on commit 1cd7967

Please sign in to comment.