Skip to content

Commit e5e6a6b

Browse files
authored
fix(angular): Filter out TryCatch integration by default (#8367)
The `TryCatch` default integration interferes with the `SentryErrorHander` error handler of the Angular(-Ivy) SDKs by catching certain errors too early, before the Angular SDK-specific error handler can catch them. This caused missing data on the event in some or duplicated errors in other cases. This fix filters out the `TryCatch` by default, as long as users didn't set `defaultIntegrations` in their SDK init. Therefore, it makes the previously provided [workaround](#5417 (comment)) obsolete.
1 parent 365c750 commit e5e6a6b

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

packages/angular-ivy/src/sdk.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VERSION } from '@angular/core';
22
import type { BrowserOptions } from '@sentry/browser';
3-
import { init as browserInit, SDK_VERSION, setContext } from '@sentry/browser';
3+
import { defaultIntegrations, init as browserInit, SDK_VERSION, setContext } from '@sentry/browser';
44
import { logger } from '@sentry/utils';
55

66
import { IS_DEBUG_BUILD } from './flags';
@@ -21,6 +21,18 @@ export function init(options: BrowserOptions): void {
2121
version: SDK_VERSION,
2222
};
2323

24+
// Filter out TryCatch integration as it interferes with our Angular `ErrorHandler`:
25+
// TryCatch would catch certain errors before they reach the `ErrorHandler` and thus provide a
26+
// lower fidelity error than what `SentryErrorHandler` (see errorhandler.ts) would provide.
27+
// see:
28+
// - https://github.com/getsentry/sentry-javascript/issues/5417#issuecomment-1453407097
29+
// - https://github.com/getsentry/sentry-javascript/issues/2744
30+
if (options.defaultIntegrations === undefined) {
31+
options.defaultIntegrations = defaultIntegrations.filter(integration => {
32+
return integration.name !== 'TryCatch';
33+
});
34+
}
35+
2436
checkAndSetAngularVersion();
2537
browserInit(options);
2638
}

packages/angular/src/sdk.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VERSION } from '@angular/core';
22
import type { BrowserOptions } from '@sentry/browser';
3-
import { init as browserInit, SDK_VERSION, setContext } from '@sentry/browser';
3+
import { defaultIntegrations, init as browserInit, SDK_VERSION, setContext } from '@sentry/browser';
44
import { logger } from '@sentry/utils';
55

66
import { IS_DEBUG_BUILD } from './flags';
@@ -21,6 +21,18 @@ export function init(options: BrowserOptions): void {
2121
version: SDK_VERSION,
2222
};
2323

24+
// Filter out TryCatch integration as it interferes with our Angular `ErrorHandler`:
25+
// TryCatch would catch certain errors before they reach the `ErrorHandler` and thus provide a
26+
// lower fidelity error than what `SentryErrorHandler` (see errorhandler.ts) would provide.
27+
// see:
28+
// - https://github.com/getsentry/sentry-javascript/issues/5417#issuecomment-1453407097
29+
// - https://github.com/getsentry/sentry-javascript/issues/2744
30+
if (options.defaultIntegrations === undefined) {
31+
options.defaultIntegrations = defaultIntegrations.filter(integration => {
32+
return integration.name !== 'TryCatch';
33+
});
34+
}
35+
2436
checkAndSetAngularVersion();
2537
browserInit(options);
2638
}

packages/angular/test/sdk.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as SentryBrowser from '@sentry/browser';
22

3-
import { init } from '../src/sdk';
3+
import { defaultIntegrations, init } from '../src/index';
44

55
describe('init', () => {
66
it('sets the Angular version (if available) in the global scope', () => {
@@ -13,4 +13,33 @@ describe('init', () => {
1313
expect(setContextSpy).toHaveBeenCalledTimes(1);
1414
expect(setContextSpy).toHaveBeenCalledWith('angular', { version: 10 });
1515
});
16+
17+
describe('filtering out the `TryCatch` integration', () => {
18+
const browserInitSpy = jest.spyOn(SentryBrowser, 'init');
19+
20+
beforeEach(() => {
21+
browserInitSpy.mockClear();
22+
});
23+
24+
it('filters if `defaultIntegrations` is not set', () => {
25+
init({});
26+
27+
expect(browserInitSpy).toHaveBeenCalledTimes(1);
28+
29+
const options = browserInitSpy.mock.calls[0][0] || {};
30+
expect(options.defaultIntegrations).not.toContainEqual(expect.objectContaining({ name: 'TryCatch' }));
31+
});
32+
33+
it.each([false as const, defaultIntegrations])(
34+
"doesn't filter if `defaultIntegrations` is set to %s",
35+
defaultIntegrations => {
36+
init({ defaultIntegrations });
37+
38+
expect(browserInitSpy).toHaveBeenCalledTimes(1);
39+
40+
const options = browserInitSpy.mock.calls[0][0] || {};
41+
expect(options.defaultIntegrations).toEqual(defaultIntegrations);
42+
},
43+
);
44+
});
1645
});

0 commit comments

Comments
 (0)