Skip to content

fix: handle empty/error bodies in TokenTester token exchange#142

Merged
antosubash merged 1 commit intomainfrom
claude/fix-token-exchange-json-3ZtOT
Apr 30, 2026
Merged

fix: handle empty/error bodies in TokenTester token exchange#142
antosubash merged 1 commit intomainfrom
claude/fix-token-exchange-json-3ZtOT

Conversation

@antosubash
Copy link
Copy Markdown
Owner

Summary

Fixes the cryptic error Token exchange failed: Failed to execute 'json' on 'Response': Unexpected end of JSON input shown by the dashboard TokenTester after an OAuth callback.

Root cause

In modules/Dashboard/src/SimpleModule.Dashboard/Pages/components/TokenTester.tsx, the /connect/token response was parsed via await res.json() unconditionally. Two failure modes:

  1. Empty body (e.g. some non-2xx responses with no body) — res.json() itself throws Unexpected end of JSON input, which becomes the user-visible error.
  2. OpenIddict error JSON (400 with { error, error_description }) — the !res.ok branch threw ${res.status} ${res.statusText} and discarded the body, hiding the actual reason for the failure (e.g. invalid_grant, PKCE mismatch, etc.).

Change

  • Read the response body as text once, then attempt JSON.parse only if non-empty.
  • On !res.ok, surface error_description / error from the parsed body when available.
  • On success, validate that access_token is present before using it.
  • Add Accept: application/json to be explicit about the expected response type.

Test plan

  • Trigger the OAuth flow from the Dashboard "Get Token" button and confirm the access token decodes successfully.
  • Force an error (e.g. tamper with the state or code_verifier) and confirm the alert now shows the OpenIddict error_description instead of the generic JSON-parse error.

Generated by Claude Code

Reading res.json() directly threw 'Unexpected end of JSON input' when
the /connect/token response body was empty, and the !res.ok branch
discarded the OpenIddict error JSON. Read the body as text once, parse
it defensively, and surface error/error_description in the alert.
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying simplemodule-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9e89fef
Status: ✅  Deploy successful!
Preview URL: https://f10719a3.simplemodule-website.pages.dev
Branch Preview URL: https://claude-fix-token-exchange-js.simplemodule-website.pages.dev

View logs

@antosubash antosubash marked this pull request as ready for review April 30, 2026 19:42
@antosubash antosubash merged commit 97b369c into main Apr 30, 2026
6 checks passed
@antosubash antosubash deleted the claude/fix-token-exchange-json-3ZtOT branch April 30, 2026 19:43
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.

2 participants