Skip to content

Commit

Permalink
Merge pull request #862 from profawxhawk/add-nil-err
Browse files Browse the repository at this point in the history
add nil error check in wrap function
  • Loading branch information
iamemilio committed Mar 6, 2024
2 parents ab5de7a + e4e5c45 commit 4b53623
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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")
)

// 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

0 comments on commit 4b53623

Please sign in to comment.