Skip to content

fix(api): handle invalid JSON gracefully#3

Merged
shenald-dev merged 1 commit into
masterfrom
fix-express-json-error-handling-16461518800795681364
Mar 18, 2026
Merged

fix(api): handle invalid JSON gracefully#3
shenald-dev merged 1 commit into
masterfrom
fix-express-json-error-handling-16461518800795681364

Conversation

@shenald-dev
Copy link
Copy Markdown
Owner

This pull request improves the reliability and security of the API gateway by explicitly handling malformed JSON payloads.

Previously, if a client sent invalid JSON (e.g., missing a closing brace), the express.json() middleware would throw a SyntaxError that would fall through to the default Express error handler, resulting in an HTML response containing a stack trace. This violates the API contract (clients expect JSON) and can leak internal server details.

This PR adds a custom error handling middleware immediately after express.json() to intercept these specific errors and return a clean {"error": "Invalid JSON payload"} with a 400 Bad Request status. It also includes an automated test to ensure this behavior is maintained.


PR created automatically by Jules for task 16461518800795681364 started by @shenald-dev

Added middleware to catch `SyntaxError` instances thrown by `express.json()` when parsing malformed JSON payloads. This prevents the server from returning an HTML stack trace and instead responds with a standard `400 Bad Request` JSON payload. Added a test to verify this behavior and documented the finding in the `.jules/bolt.md` journal.

Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com>
@shenald-dev shenald-dev merged commit e2378d8 into master Mar 18, 2026
2 checks passed
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