Skip to content

fix: handle a single JSON-RPC error response to an http batch - #5272

Closed
erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/http-batch-single-error
Closed

erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/http-batch-single-error

Conversation

@erkancamli

Copy link
Copy Markdown
Contributor

Problem

With batch enabled, the http transport assumes the response is an array and maps data[i] to each request. When a server rejects the whole batch it replies with a single JSON-RPC error object instead (the JSON-RPC 2.0 spec requires this for a batch it cannot process, and providers do it for rate limits and batch size limits). Since getHttpRpcClient now passes non-2xx JSON-RPC error bodies through (68e5841), an HTTP 429 with a JSON-RPC body takes the same path.

data[i] is then undefined, destructuring it throws, and every request in the batch fails with:

UnknownRpcError: An unknown RPC error occurred.
Details: Cannot read properties of undefined (reading 'error')

The provider's actual error is lost, and because UnknownRpcError is retryable, a non-retryable error like -32600 is retried retryCount times (4 HTTP calls instead of 1 by default).

Repro

// server replies to any batch with:
// { "jsonrpc": "2.0", "id": null, "error": { "code": -32600, "message": "batch size too large" } }
const transport = http(url, { batch: true })({})
await Promise.all([
  transport.request({ method: 'eth_blockNumber' }),
  transport.request({ method: 'eth_chainId' }),
])
// UnknownRpcError ... Cannot read properties of undefined (reading 'error')

Fix

In the batch fn of the http transport, when the response is not an array but carries an error, hand that response to every request in the batch. Each request then rejects with the proper RPC error (InvalidRequestRpcError, LimitExceededRpcError, ...) and the normal retry rules apply.

Tests

Added two cases to src/clients/transports/http.test.ts: a 200 response with a single -32600 error (asserts InvalidRequestRpcError, its details, and that only one HTTP call is made) and a 429 response with a single -32005 error (asserts LimitExceededRpcError). Both fail on main and pass with the fix.

@vercel

vercel Bot commented Oct 23, 2026

Copy link
Copy Markdown

@erkancamli is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Oct 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a77d8c9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/viem@5272

commit: a77d8c9

@jxom

jxom commented Oct 24, 2026

Copy link
Copy Markdown
Member

Thanks for the report and tests. Superseded by #5282, which also covers geth's one-element array reply; your tests are included with co-author credit.

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.

2 participants