From 27863f81d1e44e8cf47c792d0110940eac0bace5 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 20 Jun 2023 10:33:31 +0200 Subject: [PATCH] fix(angular): Filter out `TryCatch` integration by default --- packages/angular-ivy/src/sdk.ts | 14 +++++++++++++- packages/angular/src/sdk.ts | 14 +++++++++++++- packages/angular/test/sdk.test.ts | 31 ++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/packages/angular-ivy/src/sdk.ts b/packages/angular-ivy/src/sdk.ts index d16d16009ca0..fcbbbce399d0 100644 --- a/packages/angular-ivy/src/sdk.ts +++ b/packages/angular-ivy/src/sdk.ts @@ -1,6 +1,6 @@ import { VERSION } from '@angular/core'; import type { BrowserOptions } from '@sentry/browser'; -import { init as browserInit, SDK_VERSION, setContext } from '@sentry/browser'; +import { defaultIntegrations, init as browserInit, SDK_VERSION, setContext } from '@sentry/browser'; import { logger } from '@sentry/utils'; import { IS_DEBUG_BUILD } from './flags'; @@ -21,6 +21,18 @@ export function init(options: BrowserOptions): void { version: SDK_VERSION, }; + // Filter out TryCatch integration as it interferes with our Angular `ErrorHandler`: + // TryCatch would catch certain errors before they reach the `ErrorHandler` and thus provide a + // lower fidelity error than what `SentryErrorHandler` (see errorhandler.ts) would provide. + // see: + // - https://github.com/getsentry/sentry-javascript/issues/5417#issuecomment-1453407097 + // - https://github.com/getsentry/sentry-javascript/issues/2744 + if (options.defaultIntegrations === undefined) { + options.defaultIntegrations = defaultIntegrations.filter(integration => { + return integration.name !== 'TryCatch'; + }); + } + checkAndSetAngularVersion(); browserInit(options); } diff --git a/packages/angular/src/sdk.ts b/packages/angular/src/sdk.ts index 4afce6259c2b..e50cece043d0 100755 --- a/packages/angular/src/sdk.ts +++ b/packages/angular/src/sdk.ts @@ -1,6 +1,6 @@ import { VERSION } from '@angular/core'; import type { BrowserOptions } from '@sentry/browser'; -import { init as browserInit, SDK_VERSION, setContext } from '@sentry/browser'; +import { defaultIntegrations, init as browserInit, SDK_VERSION, setContext } from '@sentry/browser'; import { logger } from '@sentry/utils'; import { IS_DEBUG_BUILD } from './flags'; @@ -21,6 +21,18 @@ export function init(options: BrowserOptions): void { version: SDK_VERSION, }; + // Filter out TryCatch integration as it interferes with our Angular `ErrorHandler`: + // TryCatch would catch certain errors before they reach the `ErrorHandler` and thus provide a + // lower fidelity error than what `SentryErrorHandler` (see errorhandler.ts) would provide. + // see: + // - https://github.com/getsentry/sentry-javascript/issues/5417#issuecomment-1453407097 + // - https://github.com/getsentry/sentry-javascript/issues/2744 + if (options.defaultIntegrations === undefined) { + options.defaultIntegrations = defaultIntegrations.filter(integration => { + return integration.name !== 'TryCatch'; + }); + } + checkAndSetAngularVersion(); browserInit(options); } diff --git a/packages/angular/test/sdk.test.ts b/packages/angular/test/sdk.test.ts index bf5ecabb0ac5..0a7244d424f7 100644 --- a/packages/angular/test/sdk.test.ts +++ b/packages/angular/test/sdk.test.ts @@ -1,6 +1,6 @@ import * as SentryBrowser from '@sentry/browser'; -import { init } from '../src/sdk'; +import { defaultIntegrations, init } from '../src/index'; describe('init', () => { it('sets the Angular version (if available) in the global scope', () => { @@ -13,4 +13,33 @@ describe('init', () => { expect(setContextSpy).toHaveBeenCalledTimes(1); expect(setContextSpy).toHaveBeenCalledWith('angular', { version: 10 }); }); + + describe('filtering out the `TryCatch` integration', () => { + const browserInitSpy = jest.spyOn(SentryBrowser, 'init'); + + beforeEach(() => { + browserInitSpy.mockClear(); + }); + + it('filters if `defaultIntegrations` is not set', () => { + init({}); + + expect(browserInitSpy).toHaveBeenCalledTimes(1); + + const options = browserInitSpy.mock.calls[0][0] || {}; + expect(options.defaultIntegrations).not.toContainEqual(expect.objectContaining({ name: 'TryCatch' })); + }); + + it.each([false as const, defaultIntegrations])( + "doesn't filter if `defaultIntegrations` is set to %s", + defaultIntegrations => { + init({ defaultIntegrations }); + + expect(browserInitSpy).toHaveBeenCalledTimes(1); + + const options = browserInitSpy.mock.calls[0][0] || {}; + expect(options.defaultIntegrations).toEqual(defaultIntegrations); + }, + ); + }); });