Skip to content

fix(integrations): capture exception if any arg to console method is an error #8671

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions packages/integrations/src/captureconsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ export class CaptureConsole implements Integration {
});

let message = safeJoin(args, ' ');
const error = args.find(arg => arg instanceof Error);
if (level === 'assert') {
if (args[0] === false) {
message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
scope.setExtra('arguments', args.slice(1));
hub.captureMessage(message);
}
} else if (level === 'error' && args[0] instanceof Error) {
hub.captureException(args[0]);
} else if (level === 'error' && error) {
hub.captureException(error);
} else {
hub.captureMessage(message);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/integrations/test/captureconsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ describe('CaptureConsole setup', () => {
expect(mockHub.captureException).toHaveBeenCalledWith(someError);
});

it('should capture exception when console logs an error object in any of the args when level set to "error"', () => {
const captureConsoleIntegration = new CaptureConsole({ levels: ['error'] });
captureConsoleIntegration.setupOnce(
() => undefined,
() => getMockHubWithIntegration(captureConsoleIntegration),
);

const someError = new Error('some error');
global.console.error('Something went wrong', someError);

expect(mockHub.captureException).toHaveBeenCalledTimes(1);
expect(mockHub.captureException).toHaveBeenCalledWith(someError);
});

it('should capture message on `console.log` when no levels are provided in constructor', () => {
const captureConsoleIntegration = new CaptureConsole();
captureConsoleIntegration.setupOnce(
Expand Down