: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(frontend): stop filing browser network failures as issues (#94761)
posthog[bot]a5a5dcda1d03
diff --git a/frontend/src/lib/api-error.test.ts b/frontend/src/lib/api-error.test.tsindex 5d4315fa79d7..f5596b10c5aa 100644--- a/frontend/src/lib/api-error.test.ts+++ b/frontend/src/lib/api-error.test.ts@@ -1,4 +1,4 @@-import { ApiError, isTransientServerError, shouldReportApiFailure } from './api-error'+import { ApiError, NetworkError, isTransientServerError, shouldReportApiFailure } from './api-error'describe('api-error', () => {describe('ApiError.fromResponse', () => {@@ -100,6 +100,25 @@ describe('api-error', () => {['a 500 backend exception', { status: 500 }, true],['a 400 validation error', { status: 400 }, true],['a 404', { status: 404 }, true],+ // A request the browser never sent. Nothing of ours failed, and grouping is stack-based,+ // so reporting these opens one issue per loader that met the same connectivity blip.+ ['a Chromium fetch failure', new TypeError('Failed to fetch'), false],+ ['a WebKit fetch failure', new TypeError('Load failed'), false],+ ['a Gecko fetch failure', new TypeError('NetworkError when attempting to fetch resource.'), false],+ // `handleFetch` stringifies the original error into the message of its fallback ApiError.+ ['a fetch failure wrapped by ApiError', new ApiError('TypeError: Failed to fetch'), false],+ // A stale chunk after a deploy words itself the same way and is a defect we can fix.+ [+ 'a missing chunk after a deploy',+ new TypeError('Failed to fetch dynamically imported module: /static/Max-A2SGX7J3.js'),+ true,+ ],+ ['a module script that would not load', new TypeError('Importing a module script failed.'), true],+ // Narrowing on the class instead of the message here would bury real crashes.+ ['an application TypeError', new TypeError('u.filter is not a function'), true],+ // The residual `network` reason can be an ad blocker, a proxy, or our own edge, so it+ // stays reportable rather than being folded into the suppression above.+ ['a classified NetworkError', new NetworkError('network'), true],// No HTTP response to excuse the failure.['an error with no status', { message: 'boom' }, true],['a thrown string', 'went wrong', true],diff --git a/frontend/src/lib/api-error.ts b/frontend/src/lib/api-error.tsindex c6c00b80b666..221ec3189255 100644--- a/frontend/src/lib/api-error.ts+++ b/frontend/src/lib/api-error.ts@@ -40,6 +40,54 @@ const HANDLED_AUTH_GATE_CODES: ReadonlySet<string> = new Set(['sensitive_action_required_reauth',])+/**+ * How each browser engine words a `fetch` that never reached the server. Chromium says "Failed to+ * fetch", WebKit "Load failed", and Gecko "NetworkError when attempting to fetch resource.".+ */+export const BROWSER_FETCH_FAILURE_MESSAGES: readonly string[] = [+ 'Failed to fetch',+ 'Load failed',+ 'NetworkError when attempting to fetch resource',+]++/**+ * A module the browser could not load, which is a defect of ours rather than connectivity: a chunk+ * an open tab still asks for went missing in a deploy. Chromium and Gecko word this by extending+ * one of the messages above, so a module failure has to be recognized before a connectivity one.+ *+ * `isChunkLoadError` answers a wider question, whether to retry the import or offer a reload, and+ * so it also treats a bare "Load failed" as a stale chunk. That guess is wrong for this decision,+ * because it would keep reporting every WebKit connectivity failure.+ */+const MODULE_LOAD_FAILURE_MESSAGES: readonly string[] = [+ 'dynamically imported module',+ 'Importing a module script failed',+]++/**+ * A `fetch` the browser refused to complete, recognized by the message the engine produced. The+ * request never reached the server, so there is no status to react to and no code path of ours to+ * fix: the cause is an ad blocker, tracking protection, DNS, a captive portal, or a connection that+ * dropped mid-request.+ *+ * The match is on the message and not on the `TypeError` class on purpose. Application bugs raise+ * status-less `TypeError`s too, such as "x is not a function", and dropping those would hide real+ * crashes.+ */+export function isBrowserNetworkFailure(error: unknown): boolean {+ if (error === null || typeof error !== 'object') {+ return false+ }+ const message = (error as { message?: unknown }).message+ if (typeof message !== 'string') {+ return false+ }+ if (MODULE_LOAD_FAILURE_MESSAGES.some((known) => message.includes(known))) {+ return false+ }+ return BROWSER_FETCH_FAILURE_MESSAGES.some((known) => message.includes(known))+}+/*** Whether a failed request is worth filing as an error tracking issue. A response the app asked* for and recovers from itself is not a defect, and reporting it buries the ones that are: every@@ -55,15 +103,26 @@ const HANDLED_AUTH_GATE_CODES: ReadonlySet<string> = new Set([* - 409 carrying a `change_request_id` — the approvals UI shows the change request it created.* - 502/503/504 — the gateway couldn't reach the backend, so application code is not at fault.*+ * Left unreported for a second reason, that there is nothing to fix:+ * - a `fetch` the browser never completed. No request reached us, so no code of ours failed, and+ * the user is told by whatever the caller renders for an empty result. Reporting these is what+ * floods the project: grouping is stack-based, so every loader that meets the same connectivity+ * blip opens an issue of its own, and one bad minute on a user's network manufactures dozens.+ ** Each of these still toasts wherever it did before, and `client_request_failure` still records* every non-OK response with its status and pathname, so failure rates stay queryable even where* no recovery runs. That event is also the better record, since an exception raised here cannot say* which endpoint failed: every `ApiError` shares this file's stack.*- * A plain 500 stays reportable on purpose, being a genuine backend exception, and so does anything- * without an HTTP status (a thrown string, a bare `Error`) — there is no response to excuse it.+ * A plain 500 stays reportable on purpose, being a genuine backend exception. So does a status-less+ * failure that is not a recognized connectivity failure, such as a thrown string, a bare `Error`,+ * or an application `TypeError`, because there is no response to excuse it. A failed module import+ * keeps reporting too, since a stale chunk after a deploy is a defect we can fix.*/export function shouldReportApiFailure(error: unknown): boolean {+ if (isBrowserNetworkFailure(error)) {+ return false+ }if (error === null || typeof error !== 'object') {return true}diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex e1f449ee6ad5..02c243d420ed 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -7,7 +7,7 @@ import { encodeParams } from 'kea-router'export type { EventSourceMessage } from '@microsoft/fetch-event-source'import posthog from 'posthog-js'-import { ApiError, NetworkError, type NetworkFailureReason } from 'lib/api-error'+import { ApiError, BROWSER_FETCH_FAILURE_MESSAGES, NetworkError, type NetworkFailureReason } from 'lib/api-error'import { ActivityLogProps } from 'lib/components/ActivityLog/ActivityLog'import { ActivityLogItem } from 'lib/components/ActivityLog/humanizeActivity'import { apiStatusLogic } from 'lib/logic/apiStatusLogic'@@ -7490,13 +7490,11 @@ function requestPathname(url: string): string {* 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`.+ *+ * Matching a bare `TypeError` is safe here in a way it would not be elsewhere, because this runs+ * only after a `fetch` call rejected. `isBrowserNetworkFailure` decides the same question about an+ * arbitrary error, so it matches the message alone.*/-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 truediff --git a/frontend/src/lib/components/HelpMenu/incidentStatusLogic.tsx b/frontend/src/lib/components/HelpMenu/incidentStatusLogic.tsxindex d8ccc5707c99..80751d7313fb 100644--- a/frontend/src/lib/components/HelpMenu/incidentStatusLogic.tsx+++ b/frontend/src/lib/components/HelpMenu/incidentStatusLogic.tsx@@ -2,6 +2,7 @@ import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, seleimport { loaders } from 'kea-loaders'import posthog from 'posthog-js'+import { isBrowserNetworkFailure } from 'lib/api-error'// eslint-disable-next-line import/no-cycleimport { superpowersLogic } from 'lib/components/Superpowers/superpowersLogic'import { preflightLogic } from 'lib/logic/preflightLogic'@@ -71,15 +72,6 @@ export interface ComponentIncidentAlert {const REFRESH_INTERVAL = 60 * 1000 * 5 // 5 minutes-// A failed `fetch` to an external host throws a `TypeError` ("Failed to fetch", "NetworkError when-// attempting to fetch resource", "Load failed", ...). These are expected and outside our control —-// ad blockers, tracking-protection extensions, DNS hiccups, brief status-page outages — so they're-// noise in error tracking rather than real defects. A genuine bug here would surface as a different-// error type (e.g. a `SyntaxError` from `response.json()`), which we still want to capture.-function isNetworkError(error: unknown): boolean {- return error instanceof TypeError-}-// incident.io component name for the PostHog AI service. An incident is treated as affecting AI only// when it tags a component with this exact name under the current region's group (see getRelevantGroupName).export const AI_COMPONENT_NAME = 'PostHog AI'@@ -291,7 +283,8 @@ export const incidentStatusLogic = kea<incidentStatusLogicType>([// hiccups, brief status-page outages. Swallow the failure (degrading to 'operational'// via the rawStatus selector). We report a reachable-but-erroring status page (non-2xx)// and unexpected errors, but skip the expected network-level failures so they don't- // pollute error tracking as a recurring issue.+ // pollute error tracking as a recurring issue. A genuine bug here surfaces as a+ // different error, such as a `SyntaxError` from `response.json()`, and still reports.try {const response = await fetch(`${STATUS_PAGE_BASE}/api/v1/summary`)if (!response.ok) {@@ -304,7 +297,7 @@ export const incidentStatusLogic = kea<incidentStatusLogicType>([const data: Summary = await response.json()return data} catch (error) {- if (!isNetworkError(error)) {+ if (!isBrowserNetworkFailure(error)) {posthog.captureException(error)}return null