fix(session-prompt): surface useful error on non-JSON response#85
Merged
Conversation
When the platform returns an HTML error page (auth-protection, 5xx, Next.js error page) the previous code printed `json: invalid character '<'` which obscured the real fault. Now: check Content-Type; on non-JSON, return an error including HTTP status + body preview (first 200 bytes, whitespace-collapsed). Also adds a writeJSON helper to existing httptest handlers so they emit Content-Type: application/json, which was previously masked by the unconditional json.NewDecoder call. Three new tests cover: GET path with HTML 200, POST path (ForwardPrompt / session prompt) with HTML 200, and body truncation for large pages. SUP-1840 follow-up #4. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
session prompt(and all other CLI commands) calledjson.NewDecoder(resp.Body).Decode(target)unconditionally after a 2xx status. When Vercel auth-protection, a Next.js error boundary, or a reverse-proxy error page returned HTML with a 2xx status, the error was the crypticdecode failed: invalid character '<' looking for beginning of value.decodeJSONResponsehelper checksContent-Typefirst. On non-JSON, returnsHTTP <status> returned non-JSON response (content-type=<ct>): <body-preview>— status, type, and a 200-byte whitespace-collapsed body excerpt.getandpostinternal methods are fixed. No public API changes. Existinghttptesthandlers updated with awriteJSONhelper that setsContent-Type: application/json(their omission was masked by the old unconditional decoder).Test plan
TestDecodeJSONResponseHTMLGET— GET path withtext/html200 response; asserts useful error messageTestDecodeJSONResponseHTMLPOST— POST path viaForwardPrompt(the exactsession promptcode path); asserts useful error messageTestDecodeJSONResponseHTMLBodyTruncated— 500+ byte HTML body stays under 350 chars in the errorafclienttests pass (updated withwriteJSONhelper)go build ./...cleanmake lint— 0 issuesgo test ./...— all packages passSUP-1840 follow-up #4.
🤖 Generated with Claude Code