Skip to content

Commit 2cc2dfb

Browse files
AbhiPrasadcadesalaberry
authored andcommitted
ref(angular): Don't include BrowserApiErrors in bundle (getsentry#11294)
ref getsentry#9508 The `BrowserApiErrors` integration interfers with Angular error handler, so don't include it in the default integrations.
1 parent c2e446e commit 2cc2dfb

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

packages/angular/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export type { ErrorHandlerOptions } from './errorhandler';
22

33
export * from '@sentry/browser';
44

5-
export { init } from './sdk';
5+
export { init, getDefaultIntegrations } from './sdk';
66
export { createErrorHandler, SentryErrorHandler } from './errorhandler';
77
export {
88
browserTracingIntegration,

packages/angular/src/sdk.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
11
import { VERSION } from '@angular/core';
22
import type { BrowserOptions } from '@sentry/browser';
3-
import { getDefaultIntegrations, init as browserInit, setContext } from '@sentry/browser';
4-
import { applySdkMetadata } from '@sentry/core';
3+
import {
4+
breadcrumbsIntegration,
5+
globalHandlersIntegration,
6+
httpContextIntegration,
7+
linkedErrorsIntegration,
8+
} from '@sentry/browser';
9+
import { init as browserInit, setContext } from '@sentry/browser';
10+
import {
11+
applySdkMetadata,
12+
dedupeIntegration,
13+
functionToStringIntegration,
14+
inboundFiltersIntegration,
15+
} from '@sentry/core';
16+
import type { Integration } from '@sentry/types';
517
import { logger } from '@sentry/utils';
618

719
import { IS_DEBUG_BUILD } from './flags';
820

21+
/**
22+
* Get the default integrations for the Angular SDK.
23+
*/
24+
export function getDefaultIntegrations(): Integration[] {
25+
// Don't include the BrowserApiErrors integration as it interferes with the Angular SDK's `ErrorHandler`:
26+
// BrowserApiErrors would catch certain errors before they reach the `ErrorHandler` and
27+
// thus provide a lower fidelity error than what `SentryErrorHandler`
28+
// (see errorhandler.ts) would provide.
29+
//
30+
// see:
31+
// - https://github.com/getsentry/sentry-javascript/issues/5417#issuecomment-1453407097
32+
// - https://github.com/getsentry/sentry-javascript/issues/2744
33+
return [
34+
inboundFiltersIntegration(),
35+
functionToStringIntegration(),
36+
breadcrumbsIntegration(),
37+
globalHandlersIntegration(),
38+
linkedErrorsIntegration(),
39+
dedupeIntegration(),
40+
httpContextIntegration(),
41+
];
42+
}
43+
944
/**
1045
* Inits the Angular SDK
1146
*/
1247
export function init(options: BrowserOptions): void {
1348
const opts = {
14-
// Filter out BrowserApiErrors integration as it interferes with our Angular `ErrorHandler`:
15-
// BrowserApiErrors would catch certain errors before they reach the `ErrorHandler` and thus provide a
16-
// lower fidelity error than what `SentryErrorHandler` (see errorhandler.ts) would provide.
17-
// see:
18-
// - https://github.com/getsentry/sentry-javascript/issues/5417#issuecomment-1453407097
19-
// - https://github.com/getsentry/sentry-javascript/issues/2744
20-
defaultIntegrations: getDefaultIntegrations(options).filter(integration => {
21-
return integration.name !== 'BrowserApiErrors';
22-
}),
49+
defaultIntegrations: getDefaultIntegrations(),
2350
...options,
2451
};
2552

packages/angular/test/sdk.test.ts

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as SentryBrowser from '@sentry/browser';
22
import { vi } from 'vitest';
3-
import { getDefaultIntegrations, init } from '../src/index';
3+
import { getDefaultIntegrations, init } from '../src/sdk';
44

55
describe('init', () => {
66
it('sets the Angular version (if available) in the global scope', () => {
@@ -14,39 +14,16 @@ describe('init', () => {
1414
expect(setContextSpy).toHaveBeenCalledWith('angular', { version: 14 });
1515
});
1616

17-
describe('filtering out the `BrowserApiErrors` integration', () => {
18-
const browserInitSpy = vi.spyOn(SentryBrowser, 'init');
17+
it('does not include the BrowserApiErrors integration', () => {
18+
const browserDefaultIntegrationsWithoutBrowserApiErrors = SentryBrowser.getDefaultIntegrations()
19+
.filter(i => i.name !== 'BrowserApiErrors')
20+
.map(i => i.name)
21+
.sort();
1922

20-
beforeEach(() => {
21-
browserInitSpy.mockClear();
22-
});
23+
const angularDefaultIntegrations = getDefaultIntegrations()
24+
.map(i => i.name)
25+
.sort();
2326

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: 'BrowserApiErrors' }));
31-
});
32-
33-
it("doesn't filter if `defaultIntegrations` is set to `false`", () => {
34-
init({ defaultIntegrations: false });
35-
36-
expect(browserInitSpy).toHaveBeenCalledTimes(1);
37-
38-
const options = browserInitSpy.mock.calls[0][0] || {};
39-
expect(options.defaultIntegrations).toEqual(false);
40-
});
41-
42-
it("doesn't filter if `defaultIntegrations` is overwritten", () => {
43-
const defaultIntegrations = getDefaultIntegrations({});
44-
init({ defaultIntegrations });
45-
46-
expect(browserInitSpy).toHaveBeenCalledTimes(1);
47-
48-
const options = browserInitSpy.mock.calls[0][0] || {};
49-
expect(options.defaultIntegrations).toEqual(defaultIntegrations);
50-
});
27+
expect(angularDefaultIntegrations).toEqual(browserDefaultIntegrationsWithoutBrowserApiErrors);
5128
});
5229
});

packages/browser/src/sdk.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import { makeFetchTransport } from './transports/fetch';
3232

3333
/** Get the default integrations for the browser SDK. */
3434
export function getDefaultIntegrations(_options: Options): Integration[] {
35+
/**
36+
* Note: Please make sure this stays in sync with Angular SDK, which re-exports
37+
* `getDefaultIntegrations` but with an adjusted set of integrations.
38+
*/
3539
return [
3640
inboundFiltersIntegration(),
3741
functionToStringIntegration(),

0 commit comments

Comments
 (0)