diff --git a/packages/core/src/js/sdk.tsx b/packages/core/src/js/sdk.tsx index 0d37fb215..99cf43980 100644 --- a/packages/core/src/js/sdk.tsx +++ b/packages/core/src/js/sdk.tsx @@ -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; + 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; diff --git a/packages/core/test/sdk.test.ts b/packages/core/test/sdk.test.ts index 931d7fd6d..783f63131 100644 --- a/packages/core/test/sdk.test.ts +++ b/packages/core/test/sdk.test.ts @@ -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://abc@def.ingest.sentry.io/1234567', + dsn: 'invalid-dsn', beforeBreadcrumb: mockBeforeBreadcrumb, }; @@ -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', () => {