Skip to content

Commit 624cdb6

Browse files
committed
Better ESM matching
1 parent e497bcd commit 624cdb6

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable no-unused-vars */
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
includeLocalVariables: true,
7+
integrations: [new Sentry.Integrations.LocalVariables({ captureAllExceptions: true })],
8+
beforeSend: event => {
9+
// eslint-disable-next-line no-console
10+
console.log(JSON.stringify(event));
11+
},
12+
});
13+
14+
class Some {
15+
two(name) {
16+
throw new Error('Enough!');
17+
}
18+
}
19+
20+
function one(name) {
21+
const arr = [1, '2', null];
22+
const obj = {
23+
name,
24+
num: 5,
25+
};
26+
27+
const ty = new Some();
28+
29+
ty.two(name);
30+
}
31+
32+
try {
33+
one('some name');
34+
} catch (e) {
35+
Sentry.captureException(e);
36+
}

packages/node-integration-tests/suites/public-api/LocalVariables/test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ describe('LocalVariables integration', () => {
5252
});
5353
});
5454

55+
test('Should include local variables with ESM', done => {
56+
expect.assertions(4);
57+
58+
const testScriptPath = path.resolve(__dirname, 'local-variables-caught.mjs');
59+
60+
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
61+
const event = JSON.parse(stdout) as Event;
62+
63+
const frames = event.exception?.values?.[0].stacktrace?.frames || [];
64+
const lastFrame = frames[frames.length - 1];
65+
66+
expect(lastFrame.function).toBe('Some.two');
67+
expect(lastFrame.vars).toEqual({ name: 'some name' });
68+
69+
const penultimateFrame = frames[frames.length - 2];
70+
71+
expect(penultimateFrame.function).toBe('one');
72+
expect(penultimateFrame.vars).toEqual({
73+
name: 'some name',
74+
arr: [1, '2', null],
75+
obj: { name: 'some name', num: 5 },
76+
ty: '<Some>',
77+
});
78+
79+
done();
80+
});
81+
});
82+
5583
test('Includes local variables for caught exceptions when enabled', done => {
5684
expect.assertions(4);
5785

packages/node/src/integrations/localvariables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export class LocalVariables implements Integration {
237237
const framePromises = callFrames.map(async ({ scopeChain, functionName, this: obj }) => {
238238
const localScope = scopeChain.find(scope => scope.type === 'local');
239239

240-
const fn = obj.className === 'global' ? functionName : `${obj.className}.${functionName}`;
240+
const fn = obj.className === 'global' || !obj.className ? functionName : `${obj.className}.${functionName}`;
241241

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

0 commit comments

Comments
 (0)