Skip to content

Commit 00fd70f

Browse files
authored
fix(node): Add availablility check on current hub to Node ContextLines integration (#8715)
Before applying ContextLines, we should check that the integration is registered on the current hub
1 parent 7e3f0dc commit 00fd70f

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

packages/node-integration-tests/suites/public-api/captureException/catched-error/test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@ test('should work inside catch block', async () => {
1515
handled: true,
1616
},
1717
stacktrace: {
18-
frames: expect.any(Array),
18+
frames: expect.arrayContaining([
19+
expect.objectContaining({
20+
context_line: " throw new Error('catched_error');",
21+
pre_context: [
22+
'',
23+
'Sentry.init({',
24+
" dsn: 'https://[email protected]/1337',",
25+
" release: '1.0',",
26+
'});',
27+
'',
28+
'try {',
29+
],
30+
post_context: ['} catch (err) {', ' Sentry.captureException(err);', '}', ''],
31+
}),
32+
]),
1933
},
2034
},
2135
],

packages/node/src/integrations/contextlines.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Event, EventProcessor, Integration, StackFrame } from '@sentry/types';
1+
import type { Event, EventProcessor, Hub, Integration, StackFrame } from '@sentry/types';
22
import { addContextToFrame } from '@sentry/utils';
33
import { readFile } from 'fs';
44
import { LRUMap } from 'lru_map';
@@ -56,8 +56,14 @@ export class ContextLines implements Integration {
5656
/**
5757
* @inheritDoc
5858
*/
59-
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void {
60-
addGlobalEventProcessor(event => this.addSourceContext(event));
59+
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
60+
addGlobalEventProcessor(event => {
61+
const self = getCurrentHub().getIntegration(ContextLines);
62+
if (!self) {
63+
return event;
64+
}
65+
return this.addSourceContext(event);
66+
});
6167
}
6268

6369
/** Processes an event and adds context lines */

packages/node/test/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ describe('SentryNode', () => {
193193
return null;
194194
},
195195
dsn,
196+
integrations: [new ContextLines()],
196197
});
197198
getCurrentHub().bindClient(new NodeClient(options));
198199
configureScope((scope: Scope) => {

0 commit comments

Comments
 (0)