Skip to content
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

feat(core): Add handled option to captureConsoleIntegration #14664

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -90,6 +90,11 @@ sentryTest('it captures console messages correctly', async ({ getLocalTestUrl, p
}),
);
expect(errorWithErrorEvent?.exception?.values?.[0].value).toBe('console error with error object');
expect(errorWithErrorEvent?.exception?.values?.[0].mechanism).toEqual({
// TODO (v9): Adjust to true after changing the integration's default value
handled: false,
type: 'console',
});
expect(traceWithErrorEvent).toEqual(
expect.objectContaining({
level: 'log',
Expand Down
18 changes: 15 additions & 3 deletions packages/core/src/integrations/captureconsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ import { GLOBAL_OBJ } from '../utils-hoist/worldwide';

interface CaptureConsoleOptions {
levels?: string[];

// TODO(v9): Flip default value to `true` and adjust JSDoc!
/**
* By default, Sentry will mark captured console messages as unhandled.
* Set this to `true` if you want to mark them as handled instead.
*
* Note: in v9 of the SDK, this option will default to `true`, meaning the default behavior will change to mark console messages as handled.
* @default false
*/
handled?: boolean;
}

const INTEGRATION_NAME = 'CaptureConsole';

const _captureConsoleIntegration = ((options: CaptureConsoleOptions = {}) => {
const levels = options.levels || CONSOLE_LEVELS;
// TODO(v9): Flip default value to `true`
const handled = options.handled != null ? options.handled : false;
Lms24 marked this conversation as resolved.
Show resolved Hide resolved

return {
name: INTEGRATION_NAME,
Expand All @@ -30,7 +42,7 @@ const _captureConsoleIntegration = ((options: CaptureConsoleOptions = {}) => {
return;
}

consoleHandler(args, level);
consoleHandler(args, level, handled);
});
},
};
Expand All @@ -41,7 +53,7 @@ const _captureConsoleIntegration = ((options: CaptureConsoleOptions = {}) => {
*/
export const captureConsoleIntegration = defineIntegration(_captureConsoleIntegration);

function consoleHandler(args: unknown[], level: string): void {
function consoleHandler(args: unknown[], level: string, handled: boolean): void {
const captureContext: CaptureContext = {
level: severityLevelFromString(level),
extra: {
Expand All @@ -54,7 +66,7 @@ function consoleHandler(args: unknown[], level: string): void {
event.logger = 'console';

addExceptionMechanism(event, {
handled: false,
handled,
type: 'console',
});

Expand Down
85 changes: 67 additions & 18 deletions packages/core/test/lib/integrations/captureconsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,29 +305,78 @@ describe('CaptureConsole setup', () => {
}).not.toThrow();
});

it("marks captured exception's mechanism as unhandled", () => {
// const addExceptionMechanismSpy = jest.spyOn(utils, 'addExceptionMechanism');
describe('exception mechanism', () => {
// TODO (v9): Flip this below after adjusting the default value for `handled` in the integration
it("marks captured exception's mechanism as unhandled by default", () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'] });
captureConsole.setup?.(mockClient);

const captureConsole = captureConsoleIntegration({ levels: ['error'] });
captureConsole.setup?.(mockClient);
const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);

const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);
const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {
exception: {
values: [{}],
},
};
addedEventProcessor(someEvent);

const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {
exception: {
values: [{}],
},
};
addedEventProcessor(someEvent);
expect(captureException).toHaveBeenCalledTimes(1);
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);

expect(captureException).toHaveBeenCalledTimes(1);
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);
expect(someEvent.exception?.values?.[0]?.mechanism).toEqual({
handled: false,
type: 'console',
});
});

it("marks captured exception's mechanism as handled if set in the options", () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'], handled: true });
captureConsole.setup?.(mockClient);

expect(someEvent.exception?.values?.[0]?.mechanism).toEqual({
handled: false,
type: 'console',
const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);

const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {
exception: {
values: [{}],
},
};
addedEventProcessor(someEvent);

expect(captureException).toHaveBeenCalledTimes(1);
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);

expect(someEvent.exception?.values?.[0]?.mechanism).toEqual({
handled: true,
type: 'console',
});
});

it("marks captured exception's mechanism as unhandled if set in the options", () => {
const captureConsole = captureConsoleIntegration({ levels: ['error'], handled: false });
captureConsole.setup?.(mockClient);

const someError = new Error('some error');
GLOBAL_OBJ.console.error(someError);

const addedEventProcessor = (mockScope.addEventProcessor as jest.Mock).mock.calls[0][0];
const someEvent: Event = {
exception: {
values: [{}],
},
};
addedEventProcessor(someEvent);

expect(captureException).toHaveBeenCalledTimes(1);
expect(mockScope.addEventProcessor).toHaveBeenCalledTimes(1);

expect(someEvent.exception?.values?.[0]?.mechanism).toEqual({
handled: false,
type: 'console',
});
});
});
});
Loading