From 0cd46d5df2c17ffcf1ed4fc52b0789201de8f465 Mon Sep 17 00:00:00 2001 From: wabicai <68363074+wabicai@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:17:29 +0800 Subject: [PATCH] fix(batch): return the node error when a whole batch is rejected A node answers a batch it rejects as a whole (size or rate limit) with a single JSON-RPC error object, as JSON-RPC 2.0 requires. BatchClient called .find() on it and every caller got 'results.find is not a function' instead of the node's error. The batch promises of a failed batch were also never deleted. Co-Authored-By: Claude Opus 5.5 --- __tests__/utils/batchClient.test.ts | 55 +++++++++++++++++++++++++++++ src/utils/batch/index.ts | 16 +++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 __tests__/utils/batchClient.test.ts diff --git a/__tests__/utils/batchClient.test.ts b/__tests__/utils/batchClient.test.ts new file mode 100644 index 000000000..86604e46b --- /dev/null +++ b/__tests__/utils/batchClient.test.ts @@ -0,0 +1,55 @@ +import { BatchClient } from '../../src/utils/batch'; +import { RPC } from '../../src/types'; +import { RpcChannel } from '../../src/channel'; +import { RpcError } from '../../src/utils/errors'; + +const createBatchClient = (request: jest.Mock) => + new BatchClient({ + nodeUrl: 'http://127.0.0.1:0', + headers: {}, + interval: 0, + baseFetch: jest.fn() as any, + transport: { request } as any, + rpcMethods: {} as RPC.Methods, + }); + +describe('BatchClient without a node', () => { + test('hands a batch-level error reply to every caller', async () => { + // JSON-RPC 2.0: a batch the server rejects as a whole gets a single response object. + const reply = { jsonrpc: '2.0', id: null, error: { code: -32600, message: 'Batch too large' } }; + const client = createBatchClient(jest.fn(async () => reply)); + + const [first, second] = await Promise.all([ + client.fetch('starknet_blockNumber'), + client.fetch('starknet_chainId'), + ]); + + expect(first.error).toEqual(reply.error); + expect(second.error).toEqual(reply.error); + }); + + test('the channel turns a batch-level error reply into an RpcError', async () => { + const channel = new RpcChannel({ nodeUrl: 'http://127.0.0.1:0', batch: 0 }); + jest.spyOn((channel as any).transport, 'request').mockResolvedValue({ + jsonrpc: '2.0', + id: null, + error: { code: -32005, message: 'Rate limit exceeded' }, + }); + + const failure = channel.getBlockNumber(); + await expect(failure).rejects.toThrow(RpcError); + await expect(failure).rejects.toThrow('Rate limit exceeded'); + }); + + test('forgets the batch once it has failed', async () => { + const client = createBatchClient(jest.fn().mockRejectedValue(new Error('network down'))); + + const results = await Promise.allSettled([ + client.fetch('starknet_blockNumber'), + client.fetch('starknet_chainId'), + ]); + + expect(results.map((r) => r.status)).toEqual(['rejected', 'rejected']); + expect((client as any).batchPromises).toEqual({}); + }); +}); diff --git a/src/utils/batch/index.ts b/src/utils/batch/index.ts index 42dfdaf12..b2eeeba0a 100644 --- a/src/utils/batch/index.ts +++ b/src/utils/batch/index.ts @@ -131,8 +131,20 @@ export class BatchClient res.id === requestId);