: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(api): resolve empty 204/205 bodies to null before reading them (#82511)
Carlos Marchal75193ee18214
diff --git a/frontend/src/lib/api.test.ts b/frontend/src/lib/api.test.tsindex 29db144c0576..d03888c51144 100644--- a/frontend/src/lib/api.test.ts+++ b/frontend/src/lib/api.test.ts@@ -336,6 +336,13 @@ describe('API helper', () => {await expect(api.get('api/environments/2/insights')).resolves.toBeNull()})+ it('resolves a 204 to null even when reading its empty body rejects', async () => {+ fakeFetch.mockResolvedValue(+ fakeResponse({ status: 204, text: () => Promise.reject(new TypeError('Load failed')) })+ )+ await expect(api.get('api/projects/2/wizard/sessions/latest/')).resolves.toBeNull()+ })+it('propagates an AbortError instead of masquerading as a null result', async () => {const abortError = new DOMException('The operation was aborted', 'AbortError')fakeFetch.mockResolvedValue(fakeResponse({ text: () => Promise.reject(abortError) }))diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex c691c14b4c07..c1fdfa57f262 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -391,6 +391,11 @@ function apiErrorFallback(response: Response, method: string, url: string): striasync function getJSONFromSuccessResponse(response: Response, method: string, url: string): Promise<any> {const requestContext = (): string =>`[${method} ${new URL(url, location.origin).pathname}] (status ${response.status})`+ // A no-content response must not depend on reading its body: some engines (in our telemetry,+ // overwhelmingly WebKit) reject `.text()` on an empty body rather than resolving to "".+ if (response.status === 204 || response.status === 205 || response.body === null) {+ return null+ }let text: stringtry {text = await response.text()