Skip to content
New issue

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

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

Already on GitHub? # to your account

add nil error check in wrap function #862

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions v3/integrations/nrpkgerrors/nrkpgerrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestWrappedStackTrace(t *testing.T) {
{Error: theta(basicError{}), ExpectTopFrame: ""},
{Error: basicNRError(basicError{}), ExpectTopFrame: ""},
{Error: withAttributes(basicError{}), ExpectTopFrame: "", ExpectAttributes: map[string]interface{}{"testAttribute": 1, "foo": 2}},
{Error: nil, ExpectTopFrame: ""},
}

for idx, tc := range testcases {
Expand Down Expand Up @@ -117,6 +118,7 @@ func TestWrappedErrorClass(t *testing.T) {
{Error: alpha(basicError{}), ExpectClass: "nrpkgerrors.basicError"},
{Error: wrapWithClass(basicError{}, "zip"), ExpectClass: "zip"},
{Error: alpha(wrapWithClass(basicError{}, "zip")), ExpectClass: "nrpkgerrors.basicError"},
{Error: nil, ExpectClass: "*errors.fundamental"},
}

for idx, tc := range testcases {
Expand Down
13 changes: 12 additions & 1 deletion v3/integrations/nrpkgerrors/nrpkgerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//
// This package improves the class and stack-trace fields of pkg/error errors
// when they are recorded with Transaction.NoticeError.
//
package nrpkgerrors

import (
Expand Down Expand Up @@ -76,10 +75,22 @@ func errorClass(e error) string {
return fmt.Sprintf("%T", cause)
}

var (
errNilError = errors.New("nil error")
)

// Wrap wraps a pkg/errors error so that when noticed by
// newrelic.Transaction.NoticeError it gives an improved stacktrace and class
// type.
func Wrap(e error) error {
if e == nil {
return newrelic.Error{
Message: errNilError.Error(),
Class: errorClass(errNilError),
Stack: stackTrace(errNilError),
}
}

attributes := make(map[string]interface{})
switch error := e.(type) {
case newrelic.Error:
Expand Down
Loading