Skip to content

fix(server)!: encode batch-level refusals as standard error responses - #2539

Closed
dinwwwh wants to merge 6 commits into
mainfrom
claude/batch-error-envelope-f32796
Closed

dinwwwh wants to merge 6 commits into
mainfrom
claude/batch-error-envelope-f32796

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Nov 23, 2026

Copy link
Copy Markdown
Member

Why

BatchHandlerPlugin answered batch-level failures with bare strings and ad-hoc statuses. A batch over RequestLimitHandlerPlugin's limit got 400 plus the error message as text, instead of 413 with the PAYLOAD_TOO_LARGE JSON, and that plugin's custom error option was dropped. On the client, every call in the batch then failed with a generic error rather than the real ORPCError.

What changed

  • Routing interceptors receive encodeError(error), and StandardHandler has a public encodeError(error). Both go through the handler's codec, so RPC and OpenAPI handlers each use their own error format.
  • Batch refusals are real errors: a malformed batch is BAD_REQUEST, a batch above maxSize is PAYLOAD_TOO_LARGE, and an ORPCError thrown while the body is read keeps its code, status, message and data. Anything else thrown there becomes BAD_REQUEST.
  • Sub-responses are encoded too: an unmatched subrequest is NOT_FOUND, and a throw in mapSubrequest/mapSubresponse is INTERNAL_SERVER_ERROR (still surfaced as an unhandled rejection).
  • createNoProcedureMatchedError() in @orpc/server/standard is shared by the batch plugin and the standard-peer adapter.
  • StandardHandlerCodec.encodeError takes only the error. No built-in codec read procedure, path or options, and an error raised outside a procedure has none.
  • The client BatchLinkPlugin needed no change: it already forwards a ≥400 batch response to each call's codec. An end-to-end test covers it.
  • Docs: a tip in the Routing Interceptors sections of the RPC and OpenAPI handler pages showing encodeError.

Before / after for an over-limit batch:

HTTP/1.1 400
Payload Too Large
HTTP/1.1 413
{"json":{"defined":false,"code":"PAYLOAD_TOO_LARGE","message":"Payload Too Large"}}

Breaking

  • Batch refusal and sub-response bodies are error JSON instead of strings, and a body-plugin error keeps its own status instead of 400.
  • "Invalid batch request body" and "Invalid batch request data parameter" are now both "Invalid batch request".
  • StandardHandlerRoutingInterceptorOptions has a required encodeError. Code that builds these options by hand, or calls next({...}) without spreading, must pass it.
  • StandardHandlerCodec.encodeError no longer receives procedure, path or options.
  • A standard-peer request that matches nothing gets the encoded NOT_FOUND instead of a string body.

Notes for review

  • The plugin returns an encoded response rather than throwing, because a throw would skip outer plugins such as CORS that add headers after next().
  • The batch plugin's own refusals have no custom error option like RequestLimitHandlerPlugin has; left out as beyond this fix.
  • pnpm check and pnpm docs:validate pass. The server, openapi, mcp and tests/batch tests pass, except one openapi-inference test ("names the nearest tsconfig…") in a package this PR does not touch; not checked against main.

Batch refusals and failed subrequests answered bare strings with ad-hoc statuses, so an
ORPCError thrown while the batch body is read, such as the Request Limit Plugin's, lost
its code, status and data.

- routing interceptors receive `encodeError`, and `StandardHandler` exposes it
- `StandardHandlerCodec.encodeError` takes `procedure`, `path` and `options` as optional
- batch refusals are BAD_REQUEST / PAYLOAD_TOO_LARGE errors, a thrown ORPCError is kept as is
- unmatched subrequests and standard-peer requests share `createNoProcedureMatchedError`
No codec read `procedure`, `path` or `options`, and an error raised outside a procedure has none.
@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
More templates

@orpc/ai-sdk

npm i https://pkg.pr.new/@orpc/ai-sdk@2539

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@2539

@orpc/bun

npm i https://pkg.pr.new/@orpc/bun@2539

@orpc/client

npm i https://pkg.pr.new/@orpc/client@2539

@orpc/cloudflare

npm i https://pkg.pr.new/@orpc/cloudflare@2539

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@2539

@orpc/effect

npm i https://pkg.pr.new/@orpc/effect@2539

@orpc/evlog

npm i https://pkg.pr.new/@orpc/evlog@2539

@orpc/hibernation

npm i https://pkg.pr.new/@orpc/hibernation@2539

@orpc/json-schema

npm i https://pkg.pr.new/@orpc/json-schema@2539

@orpc/experimental-lock

npm i https://pkg.pr.new/@orpc/experimental-lock@2539

@orpc/mcp

npm i https://pkg.pr.new/@orpc/mcp@2539

@orpc/msw

npm i https://pkg.pr.new/@orpc/msw@2539

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@2539

@orpc/next

npm i https://pkg.pr.new/@orpc/next@2539

@orpc/node

npm i https://pkg.pr.new/@orpc/node@2539

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@2539

@orpc/experimental-openapi-inference

npm i https://pkg.pr.new/@orpc/experimental-openapi-inference@2539

@orpc/opentelemetry

npm i https://pkg.pr.new/@orpc/opentelemetry@2539

@orpc/pinia-colada

npm i https://pkg.pr.new/@orpc/pinia-colada@2539

@orpc/pino

npm i https://pkg.pr.new/@orpc/pino@2539

@orpc/publisher

npm i https://pkg.pr.new/@orpc/publisher@2539

@orpc/ratelimit

npm i https://pkg.pr.new/@orpc/ratelimit@2539

@orpc/server

npm i https://pkg.pr.new/@orpc/server@2539

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@2539

@orpc/swr

npm i https://pkg.pr.new/@orpc/swr@2539

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@2539

@orpc/trpc

npm i https://pkg.pr.new/@orpc/trpc@2539

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@2539

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@2539

commit: 0a616d9

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • Routing interceptors can now encode unmatched requests as standard error responses, such as NOT_FOUND.
    • Batch request failures now return structured error responses that preserve applicable error codes, messages, and details.
  • Bug Fixes
    • Standard request handling now uses the configured error encoder for unmatched requests instead of always returning a hard-coded 404.
    • Oversized batches return a PAYLOAD_TOO_LARGE error before procedures are invoked.
  • Documentation
    • Added examples showing how to handle unmatched requests with routing interceptors.

The standard handler now exposes error encoding to routing interceptors. The peer adapter and batch plugin use it to return encoded errors for unmatched requests and batch failures. Documentation and tests cover the callback and the updated batch responses.

Changes: Standard Error Encoding

Layer / File(s) Summary
Expose error encoding to interceptors
packages/server/src/adapters/standard/{codec.ts,handler.ts,rpc-handler-codec.ts,utils.ts}, packages/server/src/adapters/standard/{handler.test.ts,rpc-handler-codec.test.ts}, packages/node/src/tmp-file-upload-handler-plugin.test.ts, apps/content/docs/{openapi,rpc}/handler.mdx
The standard handler exposes an encodeError callback to routing interceptors. The codec now accepts only the error, and a shared factory creates the NOT_FOUND error for unmatched procedures. Tests and documentation cover the callback.
Encode unmatched peer requests
packages/server/src/adapters/standard-peer/utils.ts, packages/server/src/adapters/standard-peer/utils.test.ts
When the peer handler returns no response, the adapter encodes a no-procedure-matched error. Tests verify the encoded response and error.
Encode batch request and subrequest errors
packages/server/src/plugins/batch.ts, packages/server/src/plugins/batch.test.ts, tests/batch/batch-plugin.test.ts
The batch plugin parses and validates messages through readMessages, then encodes parsing, validation, size-limit, unmatched-subrequest, and internal errors. Tests check structured error bodies and configured request-limit errors.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: aldotestino

Merge Risk: Minimal · up to 5e2c4

Batch refusals and unmatched peer requests now return codec-encoded error responses instead of plain text. This is an intentional, documented breaking change. No concrete defect was found in the callers or implementers of the changed encodeError contract, so the change looks ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: batch-level refusals now return standard encoded error responses instead of plain-text bodies. The changeset matches this. It also flags the breaking change with the '…
Description check Passed The description states the problem, the changes, breaking changes, and review notes. Each part matches the changeset: batch error encoding, the new encodeError on routing interceptors and StandardHand…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: batch-level refusals now return standard encoded error responses instead of plain-text bodies. The changeset matches this. It also flags the breaking change with the '!' marker. The title is concise and specific.

Full details: Description check

Explanation

The description states the problem, the changes, breaking changes, and review notes. Each part matches the changeset: batch error encoding, the new encodeError on routing interceptors and StandardHandler, the shared NOT_FOUND helper, and the docs tips.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bdf88759-2079-4b9b-9634-e7014463f56e
Commits

Reviewing files that changed from the base of the PR and between 3ebfb98 and 5e2c46f.

Files selected for processing: 14

  • apps/content/docs/openapi/handler.mdx
  • apps/content/docs/rpc/handler.mdx
  • packages/node/src/tmp-file-upload-handler-plugin.test.ts
  • packages/server/src/adapters/standard-peer/utils.test.ts
  • packages/server/src/adapters/standard-peer/utils.ts
  • packages/server/src/adapters/standard/codec.ts
  • packages/server/src/adapters/standard/handler.test.ts
  • packages/server/src/adapters/standard/handler.ts
  • packages/server/src/adapters/standard/rpc-handler-codec.test.ts
  • packages/server/src/adapters/standard/rpc-handler-codec.ts
  • packages/server/src/adapters/standard/utils.ts
  • packages/server/src/plugins/batch.test.ts
  • packages/server/src/plugins/batch.ts
  • tests/batch/batch-plugin.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 15.57%

2 warnings

⚠️ 5 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 2 improved benchmarks
✅ 38 untouched benchmarks
⏩ 2 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ range request 2.4 ms 2 ms +19.32%
⚡ serve deeply nested encoded path 2.3 ms 2 ms +11.94%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/batch-error-envelope-f32796 (0a616d9) with main (3ebfb98)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
🔵 In progress
View logs
orpc 0a616d9 Nov 23 2026, 08:12 AM

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Minor suggestion only — one narrow behavior change to check.

Approach: sound — routing batch refusals through the handler's own codec means RPC and OpenAPI clients decode a real ORPCError instead of failing on a plain-string body, and it reuses the existing error encoding rather than adding a second format.

Reviewed changes — all 14 changed files, focused on the codec narrowing, the batch refusal paths, and the peer no-match path.

  • encodeError on the handler and routing interceptors — StandardHandler exposes encodeError(error) and passes it into routing interceptor options; StandardHandlerCodec.encodeError narrows to just the error.
  • Batch refusals are encoded errors — malformed, missing-data, and wrong-method batches become BAD_REQUEST, over-limit batches PAYLOAD_TOO_LARGE, and a body plugin's ORPCError keeps its code, status, message and data.
  • Subresponses and peers — unmatched subrequests and unmatched peer requests return createNoProcedureMatchedError() encoded through the codec; throws in mapSubrequest/mapSubresponse become encoded 500s.
  • Docs and tests — tips on the RPC/OpenAPI handler pages, updated unit tests, and an end-to-end refusal test through BatchLinkPlugin.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Comment thread packages/server/src/plugins/batch.ts Resolved
@dinwwwh dinwwwh closed this Nov 23, 2026

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — the single commit since the prior pullfrog review (0a616d9), focused on the batch maxSize failure path it fixes.

  • maxSize failures propagate again — the size check moved out of readMessages' refusal try and back into the routing interceptor body, so a throwing maxSize callback rejects handler.handle with the original error instead of being encoded as a BAD_REQUEST refusal; readMessages now takes only the request, and the check still runs exactly once per batch.
  • Regression test — a new case asserts handler.handle rejects with the exact error thrown by maxSize, covering the failure mode the prior review called out.

Verified packages/server/src/plugins/batch.test.ts (71 tests) passes locally.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — the delta since the prior pullfrog review (5e2c46f0), in packages/server/src/plugins/batch.ts and batch.test.ts.

  • maxSize failures propagate again — the size check moved out of the refusal try and back into the interceptor body, so a throwing or rejecting maxSize Value surfaces from handle instead of being encoded as 400 BAD_REQUEST, matching pre-PR behavior; a regression test asserts the original error is rethrown.
  • readMessages narrowed to the request — the helper now takes StandardLazyRequest and no longer evaluates maxSize, leaving only body/parse failures in the refusal path.

Verified with pnpm vitest run packages/server/src/plugins/batch.test.ts (71 passed).

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

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