Skip to content

ref(core): Log debug message when capturing error events #14701

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 5 commits into from
Dec 16, 2024
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
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module.exports = [
path: 'packages/browser/build/npm/esm/index.js',
import: createImport('init', 'feedbackIntegration'),
gzip: true,
limit: '41 KB',
limit: '42 KB',
},
{
name: '@sentry/browser (incl. sendFeedback)',
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { isParameterizedString, isPlainObject, isPrimitive, isThenable } from '.
import { consoleSandbox, logger } from './utils-hoist/logger';
import { checkOrSetAlreadyCaught, uuid4 } from './utils-hoist/misc';
import { SyncPromise, rejectedSyncPromise, resolvedSyncPromise } from './utils-hoist/syncpromise';
import { getPossibleEventMessages } from './utils/eventUtils';
import { parseSampleRate } from './utils/parseSampleRate';
import { prepareEvent } from './utils/prepareEvent';
import { showSpanDropWarning } from './utils/spanUtils';
Expand Down Expand Up @@ -713,6 +714,10 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
* @param scope
*/
protected _captureEvent(event: Event, hint: EventHint = {}, scope?: Scope): PromiseLike<string | undefined> {
if (DEBUG_BUILD && isErrorEvent(event)) {
logger.log(`Captured error event \`${getPossibleEventMessages(event)[0] || '<unknown>'}\``);
}

return this._processEvent(event, hint, scope).then(
finalEvent => {
return finalEvent.event_id;
Expand Down
30 changes: 2 additions & 28 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { defineIntegration } from '../integration';
import { logger } from '../utils-hoist/logger';
import { getEventDescription } from '../utils-hoist/misc';
import { stringMatchesSomePattern } from '../utils-hoist/string';
import { getPossibleEventMessages } from '../utils/eventUtils';

// "Script error." is hard coded into browsers for errors that it can't read.
// this is the result of a script being pulled in from an external domain and CORS.
Expand Down Expand Up @@ -117,7 +118,7 @@ function _isIgnoredError(event: Event, ignoreErrors?: Array<string | RegExp>): b
return false;
}

return _getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors));
return getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors));
}

function _isIgnoredTransaction(event: Event, ignoreTransactions?: Array<string | RegExp>): boolean {
Expand Down Expand Up @@ -147,33 +148,6 @@ function _isAllowedUrl(event: Event, allowUrls?: Array<string | RegExp>): boolea
return !url ? true : stringMatchesSomePattern(url, allowUrls);
}

function _getPossibleEventMessages(event: Event): string[] {
const possibleMessages: string[] = [];

if (event.message) {
possibleMessages.push(event.message);
}

let lastException;
try {
// @ts-expect-error Try catching to save bundle size
lastException = event.exception.values[event.exception.values.length - 1];
} catch (e) {
// try catching to save bundle size checking existence of variables
}

if (lastException) {
if (lastException.value) {
possibleMessages.push(lastException.value);
if (lastException.type) {
possibleMessages.push(`${lastException.type}: ${lastException.value}`);
}
}
}

return possibleMessages;
}

function _isSentryError(event: Event): boolean {
try {
// @ts-expect-error can't be a sentry error if undefined
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/utils/eventUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Event } from '../types-hoist';

/**
* Get a list of possible event messages from a Sentry event.
*/
export function getPossibleEventMessages(event: Event): string[] {
const possibleMessages: string[] = [];

if (event.message) {
possibleMessages.push(event.message);
}

try {
// @ts-expect-error Try catching to save bundle size
const lastException = event.exception.values[event.exception.values.length - 1];
if (lastException && lastException.value) {
possibleMessages.push(lastException.value);
if (lastException.type) {
possibleMessages.push(`${lastException.type}: ${lastException.value}`);
}
}
} catch (e) {
// ignore errors here
}

return possibleMessages;
}
46 changes: 46 additions & 0 deletions packages/core/test/lib/baseclient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,22 @@ describe('BaseClient', () => {
// `captureException` should bail right away this second time around and not get as far as calling this again
expect(clientEventFromException).toHaveBeenCalledTimes(1);
});

test('captures logger message', () => {
const logSpy = jest.spyOn(loggerModule.logger, 'log').mockImplementation(() => undefined);

const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
const client = new TestClient(options);

client.captureException(new Error('test error here'));
client.captureException({});

expect(logSpy).toHaveBeenCalledTimes(2);
expect(logSpy).toBeCalledWith('Captured error event `test error here`');
expect(logSpy).toBeCalledWith('Captured error event `<unknown>`');

logSpy.mockRestore();
});
});

describe('captureMessage', () => {
Expand Down Expand Up @@ -442,6 +458,20 @@ describe('BaseClient', () => {
}),
);
});

test('captures logger message', () => {
const logSpy = jest.spyOn(loggerModule.logger, 'log').mockImplementation(() => undefined);

const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
const client = new TestClient(options);

client.captureMessage('test error here');

expect(logSpy).toHaveBeenCalledTimes(1);
expect(logSpy).toBeCalledWith('Captured error event `test error here`');

logSpy.mockRestore();
});
});

describe('captureEvent() / prepareEvent()', () => {
Expand Down Expand Up @@ -1658,6 +1688,22 @@ describe('BaseClient', () => {
message: 'hello',
});
});

test('captures logger message', () => {
const logSpy = jest.spyOn(loggerModule.logger, 'log').mockImplementation(() => undefined);

const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
const client = new TestClient(options);

client.captureEvent({ message: 'hello' });
// transactions are ignored and not logged
client.captureEvent({ type: 'transaction', message: 'hello 2' });

expect(logSpy).toHaveBeenCalledTimes(1);
expect(logSpy).toBeCalledWith('Captured error event `hello`');

logSpy.mockRestore();
});
});

describe('integrations', () => {
Expand Down
Loading