fix(start-server-core): return 400 for invalid JSON server function payloads #8285
main fromrefs/pull/8285/head·opened 🎯 Changes#
A server function request with malformed JSON currently 500s. GET JSON.parse(payloadParam) and POST request.json() throw SyntaxError, the handler logs Server Fn Error!, and the client sees an unhandled 500. That's a bad request, not a server fault — crawlers replaying /_serverFn/... URLs with garbage payloads show up in production monitoring as 500s.
handleServerAction already returns a thrown Response from its outer catch, so both parse sites now throw new Response('Invalid server function payload', { status: 400 }) on SyntaxError and rethrow anything else. seroval/fromJSON still 500s; this only covers invalid JSON.
This is the malformed-JSON case from #8237. The missing x-tsr-serverFn path is already in #8220, so this PR does not close that issue.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
- I fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
Bug Fixes
- Invalid JSON in server function GET payloads and request bodies now returns a clear
400response instead of an unhandled500error. - Server functions are no longer invoked when their request payload contains malformed JSON.
- Invalid JSON in server function GET payloads and request bodies now returns a clear
Tests
- Added coverage for malformed JSON in both GET and POST server function requests.
- 1
Hello - soft nudge on this PR when bandwidth allows.
Malformed JSON on a server-function request was bubbling as a
500; this returns a400ResponseforSyntaxErroron both the GET and POST parse paths so crawler junk stops looking like a server fault.It only covers the invalid-JSON case called out in #8237, with a focused test and changeset in
start-server-core, and leavesseroval/fromJSONbehavior alone.I am happy to adjust the error body, expand coverage, or rebase if
mainhas moved.Whenever you get a chance is fine - the branch is still green and ready on my side.
devtechedge(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Team
Run ID:
3b7e9bb7-89b5-46f3-9ef5-81c2caf4607e📥 Commits
Reviewing files that changed from the base of the PR and between aee42c6b641d8e919bfed9e0f04bb1b72092f3a6 and 2df04c8cad1dc174390783c1bd3f8477afdd7046.
📒 Files selected for processing (3)
.changeset/clear-json-payload-400.mdpackages/start-server-core/src/server-functions-handler.tspackages/start-server-core/tests/server-functions-handler-invalid-json.test.tsIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
Server function handling now returns a 400 response for malformed JSON in GET payloads and JSON request bodies. Tests verify that invalid inputs do not invoke the server action. A patch changeset documents the package release.
Changes#
Invalid JSON payload handling
packages/start-server-core/src/server-functions-handler.tsSyntaxErrorand return a 400 response. Other errors are re-thrown.packages/start-server-core/tests/server-functions-handler-invalid-json.test.ts,.changeset/clear-json-payload-400.mdEstimated code review effort: 2 (Simple) | ~10 minutes
Merge Risk: ⚪ Minimal · up to
2df04Malformed server-function JSON now receives a clear 400 response rather than an internal error, without executing the action. The changed behavior is covered for GET and POST payloads and is ready to merge.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share