import * as z from 'zod/mini' import * as Relay from '../../test/Relay.js' describe('request errors', () => { test('returns Parse error for invalid JSON', async () => { const server = await Relay.createServer((await Relay.relay()).listener) try { const response = await fetch(server.url, { body: '{', headers: { 'Content-Type': 'application/json' }, method: 'POST', }) expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": -32700, "message": "Parse error", }, "id": null, "jsonrpc": "2.0", } `) } finally { await server.closeAsync() } }) test.each([ ['non-object request', 'null'], ['invalid version', JSON.stringify({ id: 1, jsonrpc: '1.0', method: 'eth_chainId' })], ['empty batch', '[]'], ])('returns Invalid Request for %s', async (_name, body) => { const server = await Relay.createServer((await Relay.relay()).listener) try { const response = await fetch(server.url, { body, headers: { 'Content-Type': 'application/json' }, method: 'POST', }) expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": -32600, "message": "Invalid Request", }, "id": null, "jsonrpc": "2.0", } `) } finally { await server.closeAsync() } }) test('returns one Invalid Request error for a malformed batch member', async () => { const server = await Relay.createServer( ( await Relay.relay({ getClient: () => { throw new Error('transport credential must not leak') }, }) ).listener, ) try { const response = await fetch(server.url, { body: JSON.stringify([ { id: 1, jsonrpc: '2.0', method: 'eth_chainId' }, { id: 2, jsonrpc: '1.0', method: 'eth_chainId' }, ]), headers: { 'Content-Type': 'application/json' }, method: 'POST', }) expect(await response.json()).toMatchInlineSnapshot(` [ { "error": { "code": -32603, "data": { "code": "internal_error", }, "message": "Internal error", }, "id": 1, "jsonrpc": "2.0", }, { "error": { "code": -32600, "message": "Invalid Request", }, "id": null, "jsonrpc": "2.0", }, ] `) } finally { await server.closeAsync() } }) test('returns bounded structured issues for invalid params', async () => { const schema = z.object({ value: z.string() }) const server = await Relay.createServer( ( await Relay.relay({ onRequest: async () => { schema.parse({ value: 1 }) }, }) ).listener, ) try { const response = await fetch(server.url, { body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'eth_chainId' }), headers: { 'Content-Type': 'application/json' }, method: 'POST', }) expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": -32602, "data": { "code": "invalid_params", "issues": [ { "message": "Invalid input", "path": [ "value", ], }, ], }, "message": "Invalid params.", }, "id": 1, "jsonrpc": "2.0", } `) } finally { await server.closeAsync() } }) })