Skip to content

Commit bf46362

Browse files
authored
Merge 10a7091 into 2442538
2 parents 2442538 + 10a7091 commit bf46362

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

packages/core/src/js/sdk.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable complexity */
22
import type { Breadcrumb, BreadcrumbHint, Integration, Scope, SendFeedbackParams, UserFeedback } from '@sentry/core';
3-
import { captureFeedback, getClient, getGlobalScope, getIntegrationsToSetup, getIsolationScope, initAndBind, logger, stackParserFromStackParserOptions, withScope as coreWithScope } from '@sentry/core';
3+
import { captureFeedback, getClient, getGlobalScope, getIntegrationsToSetup, getIsolationScope, initAndBind, logger, makeDsn, stackParserFromStackParserOptions, withScope as coreWithScope } from '@sentry/core';
44
import {
55
defaultStackParser,
66
makeFetchTransport,
@@ -66,13 +66,13 @@ export function init(passedOptions: ReactNativeOptions): void {
6666
if (!dsn) {
6767
return undefined;
6868
}
69-
try {
70-
const url = new URL(dsn);
71-
return `${url.protocol}//${url.host}`;
72-
} catch (e) {
73-
logger.error('Failed to extract url from DSN', e);
69+
const dsnComponents = makeDsn(dsn);
70+
if (!dsnComponents) {
71+
logger.error('Failed to extract url from DSN: ', dsn);
7472
return undefined;
7573
}
74+
const port = dsnComponents.port ? `:${dsnComponents.port}` : '';
75+
return `${dsnComponents.protocol}://${dsnComponents.host}${port}`;
7676
};
7777

7878
const userBeforeBreadcrumb = safeFactory(passedOptions.beforeBreadcrumb, { loggerMessage: 'The beforeBreadcrumb threw an error' });

packages/core/test/sdk.test.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,21 +345,15 @@ describe('Tests the SDK functionality', () => {
345345
expect(result).toBeNull();
346346
});
347347

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

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

355-
// Mock the URL constructor to throw an exception for this test case
356-
const originalURL = (global as any).URL;
357-
jest.spyOn(global as any, 'URL').mockImplementationOnce(() => {
358-
throw new Error('Failed to parse DSN URL');
359-
});
360-
361355
const passedOptions = {
362-
dsn: 'https://[email protected]/1234567',
356+
dsn: 'invalid-dsn',
363357
beforeBreadcrumb: mockBeforeBreadcrumb,
364358
};
365359

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

375369
expect(result).toEqual(breadcrumb);
376-
377-
// Restore the original URL constructor
378-
(global as any).URL = originalURL;
379370
});
380371

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

0 commit comments

Comments
 (0)