Skip to content

Commit 4625569

Browse files
authored
ref(core): Log debug message when capturing error events (#14701)
1 parent b9899bb commit 4625569

File tree

5 files changed

+81
-29
lines changed

5 files changed

+81
-29
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module.exports = [
9393
path: 'packages/browser/build/npm/esm/index.js',
9494
import: createImport('init', 'feedbackIntegration'),
9595
gzip: true,
96-
limit: '41 KB',
96+
limit: '42 KB',
9797
},
9898
{
9999
name: '@sentry/browser (incl. sendFeedback)',

packages/core/src/baseclient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { isParameterizedString, isPlainObject, isPrimitive, isThenable } from '.
4949
import { consoleSandbox, logger } from './utils-hoist/logger';
5050
import { checkOrSetAlreadyCaught, uuid4 } from './utils-hoist/misc';
5151
import { SyncPromise, rejectedSyncPromise, resolvedSyncPromise } from './utils-hoist/syncpromise';
52+
import { getPossibleEventMessages } from './utils/eventUtils';
5253
import { parseSampleRate } from './utils/parseSampleRate';
5354
import { prepareEvent } from './utils/prepareEvent';
5455
import { showSpanDropWarning } from './utils/spanUtils';
@@ -713,6 +714,10 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
713714
* @param scope
714715
*/
715716
protected _captureEvent(event: Event, hint: EventHint = {}, scope?: Scope): PromiseLike<string | undefined> {
717+
if (DEBUG_BUILD && isErrorEvent(event)) {
718+
logger.log(`Captured error event \`${getPossibleEventMessages(event)[0] || '<unknown>'}\``);
719+
}
720+
716721
return this._processEvent(event, hint, scope).then(
717722
finalEvent => {
718723
return finalEvent.event_id;

packages/core/src/integrations/inboundfilters.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defineIntegration } from '../integration';
55
import { logger } from '../utils-hoist/logger';
66
import { getEventDescription } from '../utils-hoist/misc';
77
import { stringMatchesSomePattern } from '../utils-hoist/string';
8+
import { getPossibleEventMessages } from '../utils/eventUtils';
89

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

120-
return _getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors));
121+
return getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors));
121122
}
122123

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

150-
function _getPossibleEventMessages(event: Event): string[] {
151-
const possibleMessages: string[] = [];
152-
153-
if (event.message) {
154-
possibleMessages.push(event.message);
155-
}
156-
157-
let lastException;
158-
try {
159-
// @ts-expect-error Try catching to save bundle size
160-
lastException = event.exception.values[event.exception.values.length - 1];
161-
} catch (e) {
162-
// try catching to save bundle size checking existence of variables
163-
}
164-
165-
if (lastException) {
166-
if (lastException.value) {
167-
possibleMessages.push(lastException.value);
168-
if (lastException.type) {
169-
possibleMessages.push(`${lastException.type}: ${lastException.value}`);
170-
}
171-
}
172-
}
173-
174-
return possibleMessages;
175-
}
176-
177151
function _isSentryError(event: Event): boolean {
178152
try {
179153
// @ts-expect-error can't be a sentry error if undefined

packages/core/src/utils/eventUtils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Event } from '../types-hoist';
2+
3+
/**
4+
* Get a list of possible event messages from a Sentry event.
5+
*/
6+
export function getPossibleEventMessages(event: Event): string[] {
7+
const possibleMessages: string[] = [];
8+
9+
if (event.message) {
10+
possibleMessages.push(event.message);
11+
}
12+
13+
try {
14+
// @ts-expect-error Try catching to save bundle size
15+
const lastException = event.exception.values[event.exception.values.length - 1];
16+
if (lastException && lastException.value) {
17+
possibleMessages.push(lastException.value);
18+
if (lastException.type) {
19+
possibleMessages.push(`${lastException.type}: ${lastException.value}`);
20+
}
21+
}
22+
} catch (e) {
23+
// ignore errors here
24+
}
25+
26+
return possibleMessages;
27+
}

packages/core/test/lib/baseclient.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ describe('BaseClient', () => {
366366
// `captureException` should bail right away this second time around and not get as far as calling this again
367367
expect(clientEventFromException).toHaveBeenCalledTimes(1);
368368
});
369+
370+
test('captures logger message', () => {
371+
const logSpy = jest.spyOn(loggerModule.logger, 'log').mockImplementation(() => undefined);
372+
373+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
374+
const client = new TestClient(options);
375+
376+
client.captureException(new Error('test error here'));
377+
client.captureException({});
378+
379+
expect(logSpy).toHaveBeenCalledTimes(2);
380+
expect(logSpy).toBeCalledWith('Captured error event `test error here`');
381+
expect(logSpy).toBeCalledWith('Captured error event `<unknown>`');
382+
383+
logSpy.mockRestore();
384+
});
369385
});
370386

371387
describe('captureMessage', () => {
@@ -442,6 +458,20 @@ describe('BaseClient', () => {
442458
}),
443459
);
444460
});
461+
462+
test('captures logger message', () => {
463+
const logSpy = jest.spyOn(loggerModule.logger, 'log').mockImplementation(() => undefined);
464+
465+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
466+
const client = new TestClient(options);
467+
468+
client.captureMessage('test error here');
469+
470+
expect(logSpy).toHaveBeenCalledTimes(1);
471+
expect(logSpy).toBeCalledWith('Captured error event `test error here`');
472+
473+
logSpy.mockRestore();
474+
});
445475
});
446476

447477
describe('captureEvent() / prepareEvent()', () => {
@@ -1658,6 +1688,22 @@ describe('BaseClient', () => {
16581688
message: 'hello',
16591689
});
16601690
});
1691+
1692+
test('captures logger message', () => {
1693+
const logSpy = jest.spyOn(loggerModule.logger, 'log').mockImplementation(() => undefined);
1694+
1695+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
1696+
const client = new TestClient(options);
1697+
1698+
client.captureEvent({ message: 'hello' });
1699+
// transactions are ignored and not logged
1700+
client.captureEvent({ type: 'transaction', message: 'hello 2' });
1701+
1702+
expect(logSpy).toHaveBeenCalledTimes(1);
1703+
expect(logSpy).toBeCalledWith('Captured error event `hello`');
1704+
1705+
logSpy.mockRestore();
1706+
});
16611707
});
16621708

16631709
describe('integrations', () => {

0 commit comments

Comments
 (0)