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

Avoid nil values when unwrapping errors #1677

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,11 @@ func (b *exceptionDataBuilder) init(e *exceptionData, err error) bool {
}
case interface{ Unwrap() []error }:
if causes := err.Unwrap(); causes != nil {
e.ErrorDetails.Cause = append(e.ErrorDetails.Cause, causes...)
for _, cause := range causes {
if cause != nil {
e.ErrorDetails.Cause = append(e.ErrorDetails.Cause, cause)
}
}
}
case interface{ Cause() error }:
if cause := err.Cause(); cause != nil {
Expand Down
27 changes: 27 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,23 @@ func TestErrorCauseUnwrap(t *testing.T) {
assert.Equal(t, "cause", payloads.Errors[0].Exception.Cause[0].Message)
}

func TestErrorCauseNilElementUnwrap(t *testing.T) {
err := errorJoin{errors.New("foo"), nil, errors.New("bar")}

tracer, recorder := transporttest.NewRecorderTracer()
defer tracer.Close()
tracer.NewError(err).Send()
tracer.Flush(nil)

payloads := recorder.Payloads()
require.Len(t, payloads.Errors, 1)
assert.Equal(t, "TestErrorCauseNilElementUnwrap", payloads.Errors[0].Culprit)

require.Len(t, payloads.Errors[0].Exception.Cause, 2)
assert.Equal(t, "foo", payloads.Errors[0].Exception.Cause[0].Message)
assert.Equal(t, "bar", payloads.Errors[0].Exception.Cause[1].Message)
}

func assertErrorTransactionSampled(t *testing.T, e model.Error, sampled bool) {
assert.Equal(t, &sampled, e.Transaction.Sampled)
if sampled {
Expand Down Expand Up @@ -691,6 +708,16 @@ func (es errorslice) Cause() error {
return es[0]
}

type errorJoin []error

func (es errorJoin) Error() string {
return "errorjoin"
}

func (es errorJoin) Unwrap() []error {
return es
}

type runtimeStackTracer struct {
message string
trace []uintptr
Expand Down
Loading