Skip to content

fix(core) : Resolve relative URLs when enhancing fetch error messages - #25555

Open
kunal649 wants to merge 3 commits into
getsentry:developfrom
kunal649:kunal/fix/fetch-error-relative-url
Open

kunal649 wants to merge 3 commits into
getsentry:developfrom
kunal649:kunal/fix/fetch-error-relative-url

Conversation

@kunal649

Copy link
Copy Markdown
Contributor

enhanceFetchErrorMessages already adds the host to generic fetch errors, so Failed to fetch becomes Failed to fetch (example.com). The catch is that it ran new URL(url) with no base, and that throws for relative URLs like /api/foo or the fetch("") from the issue. The try/catch swallowed the error, so those requests never got the host.

Now relative URLs are resolved against location.origin if there is one (the browser tracing code already passes the same thing as urlBase). Absolute URLs ignore the base, and where there's no location (Node, workers) nothing changes. I also skip the enhancement when the resolved host is empty, like on file:// pages, otherwise you'd get Failed to fetch (). location is added to InternalGlobal for the typing.

I added six unit tests (relative URL, empty URL, no location, file:// origin, opaque null origin, absolute URL on another origin).

This only touches the error message. The empty URL in the fetch breadcrumbs is a separate spot, so I left it out to discuss on the issue thread.

Fixes #13168

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit efe30f6. Configure here.

Comment thread packages/core/src/instrument/fetch.ts Outdated

@s1gr1d s1gr1d left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you so much for picking this up!

a small nit regarding the tests: they are all quite similar so you could use an it.each there, like this:

    it.each([
      ['a relative path', 'https://app.example.com/dashboard?tab=billing', '/api/users/42', 'Failed to fetch (app.example.com)'],
    ])('resolves %s against location.href', async (_name, href, target, expected) => {}

Comment thread packages/core/src/utils/worldwide.ts Outdated
/** Internal global with common properties and Sentry extensions */
export type InternalGlobal = {
navigator?: { userAgent?: string; maxTouchPoints?: number };
location?: { origin?: string };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

following the other comment

Suggested change
location?: { origin?: string };
location?: { href?: string };

@kunal649
kunal649 force-pushed the kunal/fix/fetch-error-relative-url branch from efe30f6 to dc615b1 Compare October 20, 2026 16:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Always attach some URL/target data point to fetch/xhr/request errors

2 participants