diff --git a/v3/integrations/nrpkgerrors/nrkpgerrors_test.go b/v3/integrations/nrpkgerrors/nrkpgerrors_test.go index 17d1d62c0..3742bf33b 100644 --- a/v3/integrations/nrpkgerrors/nrkpgerrors_test.go +++ b/v3/integrations/nrpkgerrors/nrkpgerrors_test.go @@ -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 { @@ -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 { diff --git a/v3/integrations/nrpkgerrors/nrpkgerrors.go b/v3/integrations/nrpkgerrors/nrpkgerrors.go index 65af40e4b..e332ffac8 100644 --- a/v3/integrations/nrpkgerrors/nrpkgerrors.go +++ b/v3/integrations/nrpkgerrors/nrpkgerrors.go @@ -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 ( @@ -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: