Skip to content

fix: keep API failures readable when a response is not JSON - #95

Open
nvtoan0201-swe wants to merge 1 commit into
CopilotKit:mainfrom
nvtoan0201-swe:fix/readable-api-errors
Open

nvtoan0201-swe wants to merge 1 commit into
CopilotKit:mainfrom
nvtoan0201-swe:fix/readable-api-errors

Conversation

@nvtoan0201-swe

Copy link
Copy Markdown

What changed

MuseApi.request and createSession parsed the body with response.json() before checking response.ok. Any failure that is not JSON — Hono's default plain-text 404, a reverse-proxy HTML error page, or an empty body — surfaced in the UI as SyntaxError: Unexpected token ... instead of the real failure.

Both paths now use a shared readApiPayload reader (apps/mobile/src/api-response.ts) that:

  • keeps a JSON error message when the server sends one;
  • falls back to a readable Request failed (<status>) (or the caller's fallback, e.g. "Could not open your workspace.") when the body is not JSON or carries no message;
  • reports a success response with no readable body clearly.

Regression coverage is in apps/mobile/test/api-response.test.ts (plain-text failure, HTML failure, JSON failure, caller fallback, empty 204, non-JSON success body).

Verification

  • pnpm test — 210 pass, 0 fail (4 new tests)
  • pnpm lint
  • pnpm typecheck (root + mobile)
  • pnpm --dir apps/worker typecheck

The change is limited to response parsing and was verified with Response fixtures. No simulator/device run.

Integration limits

No provider, platform export or container path is exercised by this change. Runtime behavior is unchanged except for the error text shown when a response is not JSON.

AI assistance was used to prepare this change; I reviewed the code, tests and checks above.

MuseApi parsed the body as JSON before checking response.ok, so a plain-text 404, an HTML proxy error page, or an empty body surfaced as a SyntaxError parse message instead of the real failure. Route session creation and requests through a shared reader that keeps JSON error messages, falls back to the response status, and reports unreadable success bodies clearly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant