From c8ee28d2b0d84135079a54f7f45131bba859dd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sat, 9 May 2026 01:27:07 +0200 Subject: [PATCH] fix(insights): map LLM-parse failure to 422 instead of 502 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same Cloudflare-rewrites-5xx footgun the v1.4.5 ai/test fix solved. A malformed JSON response from the upstream model returned HTTP 502, which Cloudflare swapped for its own HTML error page; the client's `await res.json()` then crashed with `Unexpected token '<', " --- src/app/api/insights/generate/route.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app/api/insights/generate/route.ts b/src/app/api/insights/generate/route.ts index 8858bbfe7..64220042a 100644 --- a/src/app/api/insights/generate/route.ts +++ b/src/app/api/insights/generate/route.ts @@ -82,7 +82,11 @@ export const POST = apiHandler(async (request: NextRequest) => { const validated = insightResultSchema.safeParse(parsed); insights = validated.success ? validated.data : parsed; } catch { - return apiError("Failed to parse AI response", 502); + // Returning 502 here triggers Cloudflare's HTML error rewrite, which + // breaks `await res.json()` on the client side. 422 stays passthrough + // so the React Query mutation can read the JSON body and surface a + // readable message. Same fix pattern as v1.4.5 ai/test. + return apiError("AI response was not valid JSON", 422); } await prisma.user.update({