Skip to content
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

ref(angular): Don't include BrowserApiErrors in bundle #11294

Merged
merged 2 commits into from
Mar 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type { ErrorHandlerOptions } from './errorhandler';

export * from '@sentry/browser';

export { init } from './sdk';
export { init, getDefaultIntegrations } from './sdk';
export { createErrorHandler, SentryErrorHandler } from './errorhandler';
export {
browserTracingIntegration,
Expand Down
49 changes: 38 additions & 11 deletions packages/angular/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
import { VERSION } from '@angular/core';
import type { BrowserOptions } from '@sentry/browser';
import { getDefaultIntegrations, init as browserInit, setContext } from '@sentry/browser';
import { applySdkMetadata } from '@sentry/core';
import {
breadcrumbsIntegration,
globalHandlersIntegration,
httpContextIntegration,
linkedErrorsIntegration,
} from '@sentry/browser';
import { init as browserInit, setContext } from '@sentry/browser';
import {
applySdkMetadata,
dedupeIntegration,
functionToStringIntegration,
inboundFiltersIntegration,
} from '@sentry/core';
import type { Integration } from '@sentry/types';
import { logger } from '@sentry/utils';

import { IS_DEBUG_BUILD } from './flags';

/**
* Get the default integrations for the Angular SDK.
*/
export function getDefaultIntegrations(): Integration[] {
Lms24 marked this conversation as resolved.
Show resolved Hide resolved
// Don't include the BrowserApiErrors integration as it interferes with the Angular SDK's `ErrorHandler`:
// BrowserApiErrors 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
return [
inboundFiltersIntegration(),
functionToStringIntegration(),
breadcrumbsIntegration(),
globalHandlersIntegration(),
linkedErrorsIntegration(),
dedupeIntegration(),
httpContextIntegration(),
];
}

/**
* Inits the Angular SDK
*/
export function init(options: BrowserOptions): void {
const opts = {
// Filter out BrowserApiErrors integration as it interferes with our Angular `ErrorHandler`:
// BrowserApiErrors 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
defaultIntegrations: getDefaultIntegrations(options).filter(integration => {
return integration.name !== 'BrowserApiErrors';
}),
defaultIntegrations: getDefaultIntegrations(),
...options,
};

Expand Down
43 changes: 10 additions & 33 deletions packages/angular/test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as SentryBrowser from '@sentry/browser';
import { vi } from 'vitest';
import { getDefaultIntegrations, init } from '../src/index';
import { getDefaultIntegrations, init } from '../src/sdk';

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

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

beforeEach(() => {
browserInitSpy.mockClear();
});
const angularDefaultIntegrations = getDefaultIntegrations()
.map(i => i.name)
.sort();

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: 'BrowserApiErrors' }));
});

it("doesn't filter if `defaultIntegrations` is set to `false`", () => {
init({ defaultIntegrations: false });

expect(browserInitSpy).toHaveBeenCalledTimes(1);

const options = browserInitSpy.mock.calls[0][0] || {};
expect(options.defaultIntegrations).toEqual(false);
});

it("doesn't filter if `defaultIntegrations` is overwritten", () => {
const defaultIntegrations = getDefaultIntegrations({});
init({ defaultIntegrations });

expect(browserInitSpy).toHaveBeenCalledTimes(1);

const options = browserInitSpy.mock.calls[0][0] || {};
expect(options.defaultIntegrations).toEqual(defaultIntegrations);
});
expect(angularDefaultIntegrations).toEqual(browserDefaultIntegrationsWithoutBrowserApiErrors);
});
});
4 changes: 4 additions & 0 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import { makeFetchTransport } from './transports/fetch';

/** Get the default integrations for the browser SDK. */
export function getDefaultIntegrations(_options: Options): Integration[] {
/**
* Note: Please make sure this stays in sync with Angular SDK, which re-exports
* `getDefaultIntegrations` but with an adjusted set of integrations.
*/
return [
inboundFiltersIntegration(),
functionToStringIntegration(),
Expand Down