Skip to content

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

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

Merged
merged 9 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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://[email protected]/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
@@ -1,7 +1,12 @@
import type { Event } from '@sentry/node';
import { parseSemver } from '@sentry/utils';
import * as childProcess from 'child_process';
import * as path from 'path';

const nodeMajor = parseSemver(process.version.slice(1)).major || 1;

const testIf = (condition: boolean, t: jest.It) => (condition ? t : t.skip);

describe('LocalVariables integration', () => {
test('Should not include local variables by default', done => {
expect.assertions(2);
Expand Down Expand Up @@ -52,6 +57,34 @@ describe('LocalVariables integration', () => {
});
});

testIf(nodeMajor > 10, 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
3 changes: 2 additions & 1 deletion packages/node/src/integrations/localvariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ 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}`;
// obj.className is undefined in ESM modules
const fn = obj.className === 'global' || !obj.className ? functionName : `${obj.className}.${functionName}`;

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