Skip to content

Commit 3316fad

Browse files
committed
mark CaptureConsole errors unhandled
1 parent 8eff0b3 commit 3316fad

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/unbound-method */
22
import type { Event, Hub, Integration } from '@sentry/types';
33
import type { ConsoleLevel } from '@sentry/utils';
4+
// import * as utils from '@sentry/utils';
45
import { addInstrumentationHandler, CONSOLE_LEVELS, GLOBAL_OBJ, originalConsoleMethods } from '@sentry/utils';
56

67
import { CaptureConsole } from '../src/captureconsole';
@@ -389,4 +390,36 @@ describe('CaptureConsole setup', () => {
389390
GLOBAL_OBJ.console.log('some message');
390391
}).not.toThrow();
391392
});
393+
394+
it("marks captured exception's mechanism as unhandled", () => {
395+
// const addExceptionMechanismSpy = jest.spyOn(utils, 'addExceptionMechanism');
396+
397+
const captureConsoleIntegration = new CaptureConsole({ levels: ['error'] });
398+
const mockHub = getMockHub(captureConsoleIntegration);
399+
captureConsoleIntegration.setupOnce(
400+
() => undefined,
401+
() => mockHub,
402+
);
403+
404+
const mockScope = mockHub.getScope();
405+
406+
const someError = new Error('some error');
407+
GLOBAL_OBJ.console.error(someError);
408+
409+
const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
410+
const someEvent: Event = {
411+
exception: {
412+
values: [{}],
413+
},
414+
};
415+
addedEventProcessor(someEvent);
416+
417+
expect(mockHub.captureException).toHaveBeenCalledTimes(1);
418+
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);
419+
420+
expect(someEvent.exception?.values?.[0].mechanism).toEqual({
421+
handled: false,
422+
type: 'console',
423+
});
424+
});
392425
});

0 commit comments

Comments
 (0)