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

fix(node): LocalVariables, Improve frame matching for ESM #7049

Merged
merged 9 commits into from
Feb 6, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-unused-vars */
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
includeLocalVariables: true,
integrations: [new Sentry.Integrations.LocalVariables({ captureAllExceptions: true })],
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

class Some {
two(name) {
throw new Error('Enough!');
}
}

function one(name) {
const arr = [1, '2', null];
const obj = {
name,
num: 5,
};

const ty = new Some();

ty.two(name);
}

try {
one('some name');
} catch (e) {
Sentry.captureException(e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ describe('LocalVariables integration', () => {
});
});

test('Should include local variables with ESM', done => {
expect.assertions(4);

const testScriptPath = path.resolve(__dirname, 'local-variables-caught.mjs');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;

const frames = event.exception?.values?.[0].stacktrace?.frames || [];
const lastFrame = frames[frames.length - 1];

expect(lastFrame.function).toBe('Some.two');
expect(lastFrame.vars).toEqual({ name: 'some name' });

const penultimateFrame = frames[frames.length - 2];

expect(penultimateFrame.function).toBe('one');
expect(penultimateFrame.vars).toEqual({
name: 'some name',
arr: [1, '2', null],
obj: { name: 'some name', num: 5 },
ty: '<Some>',
});

done();
});
});

test('Includes local variables for caught exceptions when enabled', done => {
expect.assertions(4);

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/integrations/localvariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class LocalVariables implements Integration {
const framePromises = callFrames.map(async ({ scopeChain, functionName, this: obj }) => {
const localScope = scopeChain.find(scope => scope.type === 'local');

const fn = obj.className === 'global' ? functionName : `${obj.className}.${functionName}`;
const fn = obj.className === 'global' || !obj.className ? functionName : `${obj.className}.${functionName}`;
timfish marked this conversation as resolved.
Show resolved Hide resolved

if (localScope?.object.objectId === undefined) {
return { function: fn };
Expand Down