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

Put Error.cause in error data with ExtraErrorData integration #7051

Closed
3 tasks done
lbogdan opened this issue Feb 3, 2023 · 3 comments
Closed
3 tasks done

Put Error.cause in error data with ExtraErrorData integration #7051

lbogdan opened this issue Feb 3, 2023 · 3 comments
Labels
Meta: Help Wanted Package: core Issues related to the Sentry Core SDK Type: Improvement

Comments

@lbogdan
Copy link

lbogdan commented Feb 3, 2023

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

7.36.0

SDK Setup

See below.

Steps to Reproduce

Given running the following script with node index.mjs:

// index.mjs
import Sentry from '@sentry/node';
import { ExtraErrorData } from '@sentry/integrations';

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  integrations: [
    new ExtraErrorData(),
  ],
});

function lower() {
  const err = new Error('lower');
  err.lowerFoo = 'baz';
  throw err;
}

function upper() {
  try {
    lower();
  } catch (lowerErr) {
    const err = new Error('upper', { cause: lowerErr });
    err.upperFoo = 'bar';
    console.error(err);
    throw err;
  }
}

upper();

Expected Result

As the errors are linked, I'd expect to see the extra data for both.

Actual Result

I can only see extra data for the upper error:

image

, although lowerFoo: 'baz' shows in the console.log() breadcrumb:

image

@lforst
Copy link
Member

lforst commented Feb 6, 2023

Hi, thanks for writing in! The reason the integration currently doesn't support cause is because that field is not enumerable and we're simply iterating over all of the fields when adding the extra data. We could probably add a case for cause with not too much effort. Currently, though, I am not sure when we will get to it but you could open a PR to speed up the process!

@lforst lforst changed the title ExtraErrorData doesn't support Error.cause Put Error.cause in error data with ExtraErrorData integration Feb 6, 2023
@lbogdan
Copy link
Author

lbogdan commented Feb 6, 2023

In the meanwhile I replaced it with a beforeSend handler where I use something like

function getExtraData(err) {
  const result = {};
  let currentErr = err;
  let keyPrefix = '';
  let empty = true;
  do {
    for (const key of Object.keys(currentErr)) {
      result[`${keyPrefix}${key}`] = currentErr[key];
      empty = false;
    }
    currentErr = currentErr.cause;
    keyPrefix += 'cause.';
} while (currentErr);
  return empty ? undefined : result;
}

which returns

{
  upperFoo: 'bar',
  'cause.lowerFoo': 'baz'
}

Another related small nit is that when the error has no extra data, an empty object {} is still added to contexts, showing an empty section in the UI:

image

@s1gr1d
Copy link
Member

s1gr1d commented Jan 15, 2025

Closed because it was added here: #9914

@s1gr1d s1gr1d closed this as completed Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Meta: Help Wanted Package: core Issues related to the Sentry Core SDK Type: Improvement
Projects
None yet
Development

No branches or pull requests

5 participants