Skip to content

Commit 9cbd70f

Browse files
committed
mark CaptureConsole errors unhandled
1 parent e06f97f commit 9cbd70f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/integrations/src/captureconsole.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { EventProcessor, Hub, Integration } from '@sentry/types';
22
import {
3+
addExceptionMechanism,
34
addInstrumentationHandler,
45
CONSOLE_LEVELS,
56
GLOBAL_OBJ,
@@ -64,6 +65,12 @@ function consoleHandler(hub: Hub, args: unknown[], level: string): void {
6465
scope.setExtra('arguments', args);
6566
scope.addEventProcessor(event => {
6667
event.logger = 'console';
68+
69+
addExceptionMechanism(event, {
70+
handled: false,
71+
type: 'console',
72+
});
73+
6774
return event;
6875
});
6976

packages/integrations/test/captureconsole.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,36 @@ describe('CaptureConsole setup', () => {
397397
GLOBAL_OBJ.console.log('some message');
398398
}).not.toThrow();
399399
});
400+
401+
it("marks captured exception's mechanism as unhandled", () => {
402+
// const addExceptionMechanismSpy = jest.spyOn(utils, 'addExceptionMechanism');
403+
404+
const captureConsoleIntegration = new CaptureConsole({ levels: ['error'] });
405+
const mockHub = getMockHub(captureConsoleIntegration);
406+
captureConsoleIntegration.setupOnce(
407+
() => undefined,
408+
() => mockHub,
409+
);
410+
411+
const mockScope = mockHub.getScope();
412+
413+
const someError = new Error('some error');
414+
GLOBAL_OBJ.console.error(someError);
415+
416+
const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
417+
const someEvent: Event = {
418+
exception: {
419+
values: [{}],
420+
},
421+
};
422+
addedEventProcessor(someEvent);
423+
424+
expect(mockHub.captureException).toHaveBeenCalledTimes(1);
425+
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);
426+
427+
expect(someEvent.exception?.values?.[0].mechanism).toEqual({
428+
handled: false,
429+
type: 'console',
430+
});
431+
});
400432
});

0 commit comments

Comments
 (0)