Skip to content

Commit

Permalink
fix: Panic when cause is nil while calling client.CaptureError (getse…
Browse files Browse the repository at this point in the history
…ntry#261)

* Return deepest cause which is not nil

* Check if CaptureError works when cause is nil
  • Loading branch information
misterEggroll authored and kamilogorek committed Jun 12, 2019
1 parent 9599e76 commit 2111b06
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
17 changes: 15 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,15 @@ func init() {
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// be returned.
//
// If the cause of the error is nil, then the original
// error will be returned.
//
// If the error is nil, nil will be returned without further
// investigation.
//
// Will return the deepest cause which is not nil.
func Cause(err error) error {
type causer interface {
Cause() error
Expand All @@ -1086,7 +1093,13 @@ func Cause(err error) error {
if !ok {
break
}
err = cause.Cause()

if _cause := cause.Cause(); _cause != nil {
err = _cause
} else {
break
}

}
return err
}
36 changes: 36 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package raven
import (
"encoding/json"
"fmt"
pkgErrors "github.com/pkg/errors"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -286,6 +287,41 @@ func TestCaptureNilError(t *testing.T) {
}
}

// Custom error which implements causer
type customErr struct {
msg string
cause error
}

func (e *customErr) Error() (errorMsg string) {
if e.msg != "" && e.cause != nil {
errorMsg = fmt.Sprintf("%v \n\t==>> %v", e.msg, e.cause)
} else if e.msg == "" && e.cause != nil {
errorMsg = fmt.Sprintf("%v", e.cause)
} else if e.msg != "" && e.cause == nil {
errorMsg = fmt.Sprintf("%s", e.msg)
}
return
}

// Implementing the causer interface from github.com/pkg/errors
func (e *customErr) Cause() error {
return e.cause
}

func TestCaptureNilCauseError(t *testing.T) {
var client = DefaultClient
err := pkgErrors.WithStack(&customErr{
// Setting a nil cause
cause: nil,
msg: "This is a test",
})
eventID := client.CaptureError(err, nil)
if eventID == "" {
t.Error("expected non-empty eventID:", eventID)
}
}

func TestNewPacketWithExtraSetsDefault(t *testing.T) {
testCases := []struct {
Extra Extra
Expand Down

0 comments on commit 2111b06

Please sign in to comment.