Guard fetch call sites against non-JSON (HTML) responses#90
Draft
posthog[bot] wants to merge 1 commit into
Draft
Guard fetch call sites against non-JSON (HTML) responses#90posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
Add a shared `fetchJson` helper that checks `response.ok` and the content-type before calling `.json()`, throwing a clean, catchable `HttpResponseError` when the body isn't JSON (e.g. a 5xx gateway page, an auth redirect, or a Django error page starting with `<!DOCTYPE html>`) instead of letting `.json()` blow up with an uncaught `SyntaxError: Unexpected token '<'`. Route the ~20 frontend fetch call sites through it, and give `Layout.tsx`'s `fetchHostname` the try/catch it was missing (the one spot that produced an uncaught error on every page load). Generated-By: PostHog Code Task-Id: 91df810a-1048-4296-9272-4630a410b5fc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The frontend threw an uncaught
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSONwhenever an API call returned an HTML page instead of JSON. Across the page components we calledawait fetch(url)and then immediatelyawait response.json()without checkingresponse.okor the content-type, so an HTML body (a gateway/error page or an auth redirect) made.json()throw.Layout.tsx'sfetchHostnameruns on every page load and had no try/catch, making it the one spot that produced a genuinely uncaught error.This adds a small shared helper and routes the fetch call sites through it so a transient HTML response degrades gracefully instead of throwing.
frontend/src/utils/api.tsexportingfetchJson(andHttpResponseError): checks the content-type before calling.json()and throws a clean, catchable error when the body isn't JSON. JSON responses are still returned as-is regardless of status code, so existing inline error-field handling keeps working.fetch().json()call sites throughfetchJson.Layout.tsx'sfetchHostnamethe missing try/catch.Non-JSON-parsing calls (a
DELETEthat readsres.text(), and the status-onlyPOSTs) are left as rawfetchsince they can't hit theSyntaxError.Why
Hardening a defensive pattern that spans the whole frontend: a stray HTML response (5xx page, redirect, Django error page) should no longer crash the UI. This is a robustness cleanup rather than a fix for an active production incident.
Test plan
tsc --noEmitreports zero errors insrc/.Created with PostHog Code from an inbox report.