Skip to content

: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.

fix(insights): stop rethrowing handled gateway timeouts on insight update (#79394)

posthog[bot]8b011b184219
diff --git a/frontend/src/lib/api-error.test.ts b/frontend/src/lib/api-error.test.ts
index 447739c8558d..ea3801aaa69c 100644
--- a/frontend/src/lib/api-error.test.ts
+++ b/frontend/src/lib/api-error.test.ts
@@ -1,57 +1,84 @@
-import { ApiError } from './api-error'
+import { ApiError, isTransientServerError } from './api-error'
-describe('ApiError.fromResponse', () => {
- it.each([
- ['error', { error: 'error message' }, 'error message'],
- ['detail', { detail: 'detail message' }, 'detail message'],
- ['message', { message: 'message value' }, 'message value'],
- ])('uses the %s field as the error message', async (_, body, expected) => {
- const response = new Response(JSON.stringify(body), { status: 400 })
+describe('api-error', () => {
+ describe('ApiError.fromResponse', () => {
+ it.each([
+ ['error', { error: 'error message' }, 'error message'],
+ ['detail', { detail: 'detail message' }, 'detail message'],
+ ['message', { message: 'message value' }, 'message value'],
+ ])('uses the %s field as the error message', async (_, body, expected) => {
+ const response = new Response(JSON.stringify(body), { status: 400 })
- const error = await ApiError.fromResponse(response, 'fallback')
+ const error = await ApiError.fromResponse(response, 'fallback')
- expect(error).toMatchObject({ message: expected, status: 400, data: body })
- })
+ expect(error).toMatchObject({ message: expected, status: 400, data: body })
+ })
- it('uses the fallback for a response without a recognized message', async () => {
- const response = new Response('Bad gateway', { status: 502 })
+ it('uses the fallback for a response without a recognized message', async () => {
+ const response = new Response('Bad gateway', { status: 502 })
- const error = await ApiError.fromResponse(response, 'Request failed')
+ const error = await ApiError.fromResponse(response, 'Request failed')
- expect(error).toMatchObject({ message: 'Request failed', status: 502, data: null })
- })
+ expect(error).toMatchObject({ message: 'Request failed', status: 502, data: null })
+ })
- it('preserves response metadata and prioritizes error messages consistently', async () => {
- const body = {
- error: 'primary message',
- detail: 'DRF detail',
- message: 'secondary message',
- code: 'permission_denied',
- }
-
- const error = await ApiError.fromResponse(new Response(JSON.stringify(body), { status: 403 }))
-
- expect(error).toMatchObject({
- message: 'primary message',
- detail: 'DRF detail',
- code: 'permission_denied',
- status: 403,
- data: body,
+ it('preserves response metadata and prioritizes error messages consistently', async () => {
+ const body = {
+ error: 'primary message',
+ detail: 'DRF detail',
+ message: 'secondary message',
+ code: 'permission_denied',
+ }
+
+ const error = await ApiError.fromResponse(new Response(JSON.stringify(body), { status: 403 }))
+
+ expect(error).toMatchObject({
+ message: 'primary message',
+ detail: 'DRF detail',
+ code: 'permission_denied',
+ status: 403,
+ data: body,
+ })
})
- })
- it('uses the default ApiError message for an empty response without a fallback', async () => {
- const error = await ApiError.fromResponse(new Response(null, { status: 404 }))
+ it('uses the default ApiError message for an empty response without a fallback', async () => {
+ const error = await ApiError.fromResponse(new Response(null, { status: 404 }))
- expect(error).toMatchObject({ message: 'API request failed with status: 404', status: 404, data: null })
+ expect(error).toMatchObject({ message: 'API request failed with status: 404', status: 404, data: null })
+ })
+
+ it('propagates an aborted response body read', async () => {
+ const abortError = new DOMException('Aborted', 'AbortError')
+ const response = {
+ json: jest.fn().mockRejectedValue(abortError),
+ } as unknown as Response
+
+ await expect(ApiError.fromResponse(response)).rejects.toBe(abortError)
+ })
})
- it('propagates an aborted response body read', async () => {
- const abortError = new DOMException('Aborted', 'AbortError')
- const response = {
- json: jest.fn().mockRejectedValue(abortError),
- } as unknown as Response
+ describe('isTransientServerError', () => {
+ it.each([
+ // The empty-bodied gateway timeout that the insight-save flows must swallow rather than rethrow.
+ ['a 503 with no body (detail null)', new ApiError(undefined, 503), true],
+ ['a 502 bad gateway', new ApiError(undefined, 502), true],
+ ['a 504 gateway timeout', new ApiError(undefined, 504), true],
+ ['a 500 application error', new ApiError('boom', 500), false],
+ ['a 599', new ApiError(undefined, 599), false],
+ ['a 400 validation error', new ApiError('bad', 400), false],
+ ['a 403', new ApiError('nope', 403), false],
+ ['a 429 rate limit', new ApiError('slow down', 429), false],
+ ['an ApiError with no status', new ApiError('mystery'), false],
+ ])('classifies %s', (_, error, expected) => {
+ expect(isTransientServerError(error)).toBe(expected)
+ })
- await expect(ApiError.fromResponse(response)).rejects.toBe(abortError)
+ it.each([
+ ['a plain Error', new Error('network down')],
+ ['null', null],
+ ['a bare object shaped like an error', { status: 503 }],
+ ])('does not classify %s as transient', (_, error) => {
+ expect(isTransientServerError(error)).toBe(false)
+ })
})
})
diff --git a/frontend/src/lib/api-error.ts b/frontend/src/lib/api-error.ts
index d99784fd69a4..62fc5a79bc10 100644
--- a/frontend/src/lib/api-error.ts
+++ b/frontend/src/lib/api-error.ts
@@ -15,6 +15,17 @@ export function isApprovalRequiredError(error: { status?: number; data?: any } |
return error?.status === 409 && Boolean(error?.data?.change_request_id)
}
+/**
+ * A transient gateway failure (502/503/504) rather than anything the caller did wrong. These
+ * often arrive with an empty body (so `detail` is null) and usually succeed on retry, so a
+ * listener that has already shown a toast should stop here instead of rethrowing into
+ * unhandled-rejection tracking. A plain 500 is excluded on purpose: it signals an application
+ * bug, so it should keep surfacing its `detail` and reach error tracking.
+ */
+export function isTransientServerError(error: unknown): boolean {
+ return error instanceof ApiError && (error.status === 502 || error.status === 503 || error.status === 504)
+}
+
export class ApiError extends Error {
/** Django REST Framework `detail` - used in downstream error handling. */
detail: string | null
diff --git a/frontend/src/scenes/insights/insightLogic.tsx b/frontend/src/scenes/insights/insightLogic.tsx
index 1a55b09e537b..fe0817ee4af8 100644
--- a/frontend/src/scenes/insights/insightLogic.tsx
+++ b/frontend/src/scenes/insights/insightLogic.tsx
@@ -19,6 +19,7 @@ import posthog from 'posthog-js'
import { LemonDialog, LemonInput } from '@posthog/lemon-ui'
import { ApiError } from 'lib/api'
+import { isTransientServerError } from 'lib/api-error'
import { tryShowMCPHint } from 'lib/components/MCPHint/mcpHintLogic'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { LemonField } from 'lib/lemon-ui/LemonField'
@@ -1067,6 +1068,13 @@ export const insightLogic: LogicWrapper<insightLogicType> = kea<insightLogicType
actions.saveInsightSuccess()
} catch (e) {
actions.saveInsightFailure()
+ if (isTransientServerError(e)) {
+ // Gateway timeouts (e.g. an empty-bodied 503) carry no actionable detail and usually
+ // succeed on retry. We've handled the failure, so stop here rather than rethrowing an
+ // already-handled error into error tracking as an unhandled rejection.
+ lemonToast.error('Saving your insight timed out. Try again in a moment.')
+ return
+ }
if (e instanceof ApiError) {
lemonToast.error(e.detail ?? 'Could not save insight')
} else {
diff --git a/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts b/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts
index 879a45617748..e6af5a5add41 100644
--- a/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts
+++ b/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts
@@ -3,6 +3,7 @@ import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import api from 'lib/api'
+import { isTransientServerError } from 'lib/api-error'
import { FEATURE_FLAGS } from 'lib/constants'
import { Sorting } from 'lib/lemon-ui/LemonTable'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
@@ -378,6 +379,13 @@ export const addSavedInsightsModalLogic = kea<addSavedInsightsModalLogicType>([
}
} catch (e) {
actions.dashboardUpdateFailed(insight.id)
+ if (isTransientServerError(e)) {
+ // Gateway timeouts (e.g. an empty-bodied 503) usually succeed on retry. We've handled
+ // the failure, so stop here rather than rethrowing into error tracking as an unhandled
+ // rejection.
+ lemonToast.error('Adding the insight to the dashboard timed out. Try again in a moment.')
+ return
+ }
lemonToast.error('Failed to add insight to dashboard')
throw e
} finally {
@@ -402,6 +410,13 @@ export const addSavedInsightsModalLogic = kea<addSavedInsightsModalLogicType>([
}
} catch (e) {
actions.dashboardUpdateFailed(insight.id)
+ if (isTransientServerError(e)) {
+ // Gateway timeouts (e.g. an empty-bodied 503) usually succeed on retry. We've handled
+ // the failure, so stop here rather than rethrowing into error tracking as an unhandled
+ // rejection.
+ lemonToast.error('Removing the insight from the dashboard timed out. Try again in a moment.')
+ return
+ }
lemonToast.error('Failed to remove insight from dashboard')
throw e
} finally {