Skip to content

Commit cf0b7fc

Browse files
authored
Merge 486c8f7 into d8e8c67
2 parents d8e8c67 + 486c8f7 commit cf0b7fc

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

packages/core/src/js/sdk.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ export function init(passedOptions: ReactNativeOptions): void {
6767
return undefined;
6868
}
6969
try {
70-
const url = new URL(dsn);
71-
return `${url.protocol}//${url.host}`;
70+
const regex = /^(https?):\/\/(?:[^@]+@)?([^/]+)(?:\/.*)?$/;
71+
const matches = dsn.match(regex);
72+
73+
if (matches) {
74+
const [, protocol, host] = matches;
75+
return `${protocol}://${host}`;
76+
}
77+
logger.error('Failed to extract url from DSN: ', dsn);
78+
return undefined;
7279
} catch (e) {
7380
logger.error('Failed to extract url from DSN', e);
7481
return undefined;

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)