Skip to content

Commit 5fa2c01

Browse files
authored
fix: empty URL should not provide the breadcrumb "undefined"
1 parent 1cd2374 commit 5fa2c01

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

packages/core/src/utils/url.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ export function getSanitizedUrlString(url: PartialURL): string {
319319
.replace(/(:80)$/, '')
320320
.replace(/(:443)$/, '') || '';
321321

322-
return `${protocol ? `${protocol}://` : ''}${filteredHost}${path}`;
322+
// `parseUrl` returns `{}` for an empty or unparseable URL, and interpolating a missing path
323+
// would render the string 'undefined'.
324+
return `${protocol ? `${protocol}://` : ''}${filteredHost}${path || ''}`;
323325
}
324326

325327
/**

packages/core/test/lib/integrations/fetch.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ describe('createFetchIntegration', () => {
253253
expect(addBreadcrumbSpy.mock.lastCall?.[0].data?.['url.query']).toBeUndefined();
254254
});
255255

256+
it('records an empty breadcrumb URL rather than the string "undefined"', () => {
257+
const handler = setupIntegration(fetchIntegration(), client);
258+
259+
handler({
260+
fetchData: { url: '', method: 'GET' },
261+
args: [''],
262+
startTimestamp: Date.now(),
263+
endTimestamp: Date.now() + 100,
264+
error: new Error('Invalid URL'),
265+
});
266+
267+
expect(addBreadcrumbSpy).toHaveBeenCalledWith(
268+
expect.objectContaining({ data: { method: 'GET', url: '' } }),
269+
expect.anything(),
270+
);
271+
});
272+
256273
it('creates an error-level breadcrumb for a failed request', () => {
257274
const handler = setupIntegration(fetchIntegration(), client);
258275

packages/core/test/lib/utils/url.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ describe('getSanitizedUrlString', () => {
7878
['url with port 4433', 'http://172.31.12.144:4433/test', 'http://172.31.12.144:4433/test'],
7979
['url with port 443', 'http://172.31.12.144:443/test', 'http://172.31.12.144/test'],
8080
['url with IP and port 80', 'http://172.31.12.144:80/test', 'http://172.31.12.144/test'],
81+
['empty url', '', ''],
82+
['unparseable url', '???', ''],
8183
])('returns a sanitized URL for a %s', (_, rawUrl: string, sanitizedURL: string) => {
8284
const urlObject = parseUrl(rawUrl);
8385
expect(getSanitizedUrlString(urlObject)).toEqual(sanitizedURL);

0 commit comments

Comments
 (0)