:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(error-tracking): classify cross-realm fetch failures as NetworkError (#86454)
posthog[bot]9ecf7877ba81
diff --git a/frontend/src/lib/api.test.ts b/frontend/src/lib/api.test.tsindex d03888c51144..128e94c3d3eb 100644--- a/frontend/src/lib/api.test.ts+++ b/frontend/src/lib/api.test.ts@@ -423,6 +423,18 @@ describe('API helper', () => {expect(error).toBeInstanceOf(NetworkError)})+ it('classifies a cross-realm fetch failure that fails instanceof TypeError', async () => {+ // A TypeError thrown in another realm (an iframe) or a `fetch` swapped by a browser+ // extension fails `instanceof TypeError`, so matching only on the class would drop it+ // to an unclassified per-endpoint ApiError. Match the name and known message instead.+ const crossRealmError = { name: 'TypeError', message: 'Failed to fetch' }+ fakeFetch.mockRejectedValue(crossRealmError)++ const error = await api.get('api/environments/2/insights').catch((e) => e)++ expect(error).toBeInstanceOf(NetworkError)+ })+it('leaves a throw that is not a fetch failure as an unclassified ApiError', async () => {// A real fault in the request path must not be relabelled as connectivity, or// `dropUnactionableNetworkExceptions` would filter it out of error tracking.diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex e586b44ee3ba..a70bcc61e431 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -7463,6 +7463,31 @@ function requestPathname(url: string): string {}}+/**+ * The browser rejects a fetch that never reached the server with a `TypeError`, but `instanceof+ * TypeError` alone misses two real cases: an error thrown in another realm (an iframe, a worker)+ * carries that realm's `TypeError`, and a `fetch` replaced by a browser extension can reject with+ * its own error shape. Both keep the class name and the engine-specific message, so we match those+ * as well before a connectivity failure falls through to an unclassified `ApiError`.+ */+const BROWSER_FETCH_FAILURE_MESSAGES = [+ 'Failed to fetch',+ 'Load failed',+ 'NetworkError when attempting to fetch resource',+]++function isBrowserFetchFailure(error: unknown): boolean {+ if (error instanceof TypeError) {+ return true+ }+ const candidate = error as { name?: unknown; message?: unknown } | null+ if (candidate?.name === 'TypeError') {+ return true+ }+ const message = candidate?.message+ return typeof message === 'string' && BROWSER_FETCH_FAILURE_MESSAGES.some((known) => message.includes(known))+}+function classifyNetworkFailure(): NetworkFailureReason {if (documentUnloading) {return 'navigating'@@ -7517,7 +7542,7 @@ async function handleFetch(// was offline or going away. Anything else thrown by the fetcher is a genuine fault in the// request path, so it keeps surfacing as an unclassified `ApiError` rather than being// relabelled as a connectivity problem and filtered out of error tracking.- if (error instanceof TypeError) {+ if (isBrowserFetchFailure(error)) {const reason = classifyNetworkFailure()captureClientRequestFailure({pathname: requestPathname(url),