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

fix(core): Use makeDsn from core to extract the URL from DSN for filtering breadcrumbs #4395

Merged
merged 10 commits into from
Jan 16, 2025
11 changes: 9 additions & 2 deletions packages/core/src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ export function init(passedOptions: ReactNativeOptions): void {
return undefined;
}
try {
const url = new URL(dsn);
return `${url.protocol}//${url.host}`;
const regex = /^(https?):\/\/(?:[^@]+@)?([^/]+)(?:\/.*)?$/;
const matches = dsn.match(regex);

if (matches) {
const [, protocol, host] = matches;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've missed that in the original review as well, but let's use the makeDsn function from sentry/core. It already uses regex and is used in the client impl, so it 100% works with RN.

https://github.com/getsentry/sentry-javascript/blob/6f56d063e479f90ce07a5ddc0636dc0bc3b3bb2e/packages/core/src/utils-hoist/dsn.ts#L122

Copy link
Collaborator Author

@antonis antonis Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea @krystofwoldrich 👍
Updated with a1ecb49 to use the makeDsn function.

return `${protocol}://${host}`;
}
logger.error('Failed to extract url from DSN: ', dsn);
return undefined;
} catch (e) {
logger.error('Failed to extract url from DSN', e);
return undefined;
Expand Down
13 changes: 2 additions & 11 deletions packages/core/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,15 @@ describe('Tests the SDK functionality', () => {
expect(result).toBeNull();
});

it('should keep breadcrumbs matching dsn if the url parsing fails for dsn', () => {
it('should keep breadcrumbs if the url parsing fails for dsn', () => {
(getDevServer as jest.Mock).mockReturnValue({ url: 'http://localhost:8081' });

const mockBeforeBreadcrumb = (breadcrumb: Breadcrumb, _hint?: BreadcrumbHint) => {
return breadcrumb;
};

// Mock the URL constructor to throw an exception for this test case
const originalURL = (global as any).URL;
jest.spyOn(global as any, 'URL').mockImplementationOnce(() => {
throw new Error('Failed to parse DSN URL');
});

const passedOptions = {
dsn: 'https://[email protected]/1234567',
dsn: 'invalid-dsn',
beforeBreadcrumb: mockBeforeBreadcrumb,
};

Expand All @@ -373,9 +367,6 @@ describe('Tests the SDK functionality', () => {
const result = usedOptions()?.beforeBreadcrumb!(breadcrumb);

expect(result).toEqual(breadcrumb);

// Restore the original URL constructor
(global as any).URL = originalURL;
});

it('should keep non dev server or dsn breadcrumbs', () => {
Expand Down
Loading