Skip to content
Source
Sign inSign up
TanStack/router

fix(start-server-core): return 400 for invalid JSON server function payloads #8285

Closeddevtechedge(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/8285/headopened

🎯 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 400 response instead of an unhandled 500 error.
    • Server functions are no longer invoked when their request payload contains malformed JSON.
  • Tests

    • Added coverage for malformed JSON in both GET and POST server function requests.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      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.md
      • packages/start-server-core/src/server-functions-handler.ts
      • packages/start-server-core/tests/server-functions-handler-invalid-json.test.ts

      Included 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

      Layer / File(s)Summary
      Payload parsing error handling
      packages/start-server-core/src/server-functions-handler.ts
      GET payload parsing and JSON body parsing now catch SyntaxError and return a 400 response. Other errors are re-thrown.
      Invalid payload validation and release metadata
      packages/start-server-core/tests/server-functions-handler-invalid-json.test.ts, .changeset/clear-json-payload-400.md
      Tests verify the 400 response message and confirm that invalid requests do not invoke the server action. The changeset records a patch release.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Merge Risk: ⚪ Minimal · up to 2df04

      Malformed 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)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring 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 …Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the main change: returning HTTP 400 for invalid JSON server function payloads.
      Description check✅ PassedThe description follows the required template. It explains the motivation and implementation, completes the checklist, and documents the release impact and changeset.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      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.)

      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      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
      Comment `@coderabbitai help` to get the list of available commands.
    2. Hello - soft nudge on this PR when bandwidth allows.

      Malformed JSON on a server-function request was bubbling as a 500; this returns a 400 Response for SyntaxError on 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 leaves seroval / fromJSON behavior alone.

      I am happy to adjust the error body, expand coverage, or rebase if main has moved.

      Whenever you get a chance is fine - the branch is still green and ready on my side.

Sign in to join this conversation.