Skip to content

fix: return 400 for malformed JSON instead of 500#7

Open
iRonin wants to merge 1 commit into
standardagents:mainfrom
iRonin:fix/parse-json-body-error
Open

fix: return 400 for malformed JSON instead of 500#7
iRonin wants to merge 1 commit into
standardagents:mainfrom
iRonin:fix/parse-json-body-error

Conversation

@iRonin
Copy link
Copy Markdown

@iRonin iRonin commented May 23, 2026

Problem

parseJsonBody() in worker/http.ts validates Content-Type but does not wrap request.json(). If the body contains malformed JSON, request.json() throws a raw SyntaxError, which bypasses HttpError handling and renders as a 500 internal_error instead of a 400.

Fix

Wrap request.json() in a try/catch that throws HttpError("Invalid JSON in request body", 400).

Files changed

  • worker/http.ts — try/catch around request.json()
  • worker/index.test.ts — test asserting 400 for malformed JSON

parseJsonBody() did not wrap request.json(), so a SyntaxError from
malformed JSON bubbled up as a 500 internal_error. Now catches the
parse failure and throws a proper HttpError(400).

- worker/http.ts: wrap request.json() in try/catch
- worker/index.test.ts: test verifying 400 response for invalid JSON
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