Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.

Commit f0369cd

Browse files
committed
feat: Handler.feePayer error handling
1 parent dc0c4a0 commit f0369cd

3 files changed

Lines changed: 102 additions & 58 deletions

File tree

.changeset/whole-lions-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tempo.ts": patch
3+
---
4+
5+
Added error handling to `Handler.feePayer`.

src/server/Handler.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,5 +785,30 @@ describe('feePayer', () => {
785785
}
786786
`)
787787
})
788+
789+
test('behavior: internal error', async () => {
790+
const response = await fetch(server.url, {
791+
method: 'POST',
792+
body: JSON.stringify({
793+
jsonrpc: '2.0',
794+
id: 1,
795+
method: 'eth_signRawTransaction',
796+
params: ['0xinvalid'],
797+
}),
798+
})
799+
800+
const data = await response.json()
801+
expect(data).toMatchInlineSnapshot(`
802+
{
803+
"error": {
804+
"code": -32603,
805+
"name": "RpcResponse.InternalError",
806+
"stack": "",
807+
},
808+
"id": 1,
809+
"jsonrpc": "2.0",
810+
}
811+
`)
812+
})
788813
})
789814
})

src/server/Handler.ts

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -521,72 +521,86 @@ export function feePayer(options: feePayer.Options) {
521521
router.post(path, async ({ request: req }) => {
522522
const request = RpcRequest.from((await req.json()) as any)
523523

524-
await onRequest?.(request)
525-
526-
if (request.method === 'eth_signTransaction') {
527-
const transactionRequest = formatTransaction(request.params?.[0] as never)
524+
try {
525+
await onRequest?.(request)
526+
527+
if (request.method === 'eth_signTransaction') {
528+
const transactionRequest = formatTransaction(
529+
request.params?.[0] as never,
530+
)
531+
532+
const serializedTransaction = await signTransaction(client, {
533+
...transactionRequest,
534+
account,
535+
// @ts-expect-error
536+
feePayer: account,
537+
})
538+
539+
return Response.json(
540+
RpcResponse.from({ result: serializedTransaction }, { request }),
541+
)
542+
}
528543

529-
const serializedTransaction = await signTransaction(client, {
530-
...transactionRequest,
531-
account,
532-
// @ts-expect-error
533-
feePayer: account,
534-
})
544+
if ((request as any).method === 'eth_signRawTransaction') {
545+
const serialized = request.params?.[0] as `0x76${string}`
546+
const transaction = Transaction.deserialize(serialized)
535547

536-
return Response.json(
537-
RpcResponse.from({ result: serializedTransaction }, { request }),
538-
)
539-
}
548+
const serializedTransaction = await signTransaction(client, {
549+
...transaction,
550+
account,
551+
// @ts-expect-error
552+
feePayer: account,
553+
})
540554

541-
if ((request as any).method === 'eth_signRawTransaction') {
542-
const serialized = request.params?.[0] as `0x76${string}`
543-
const transaction = Transaction.deserialize(serialized)
555+
return Response.json(
556+
RpcResponse.from({ result: serializedTransaction }, { request }),
557+
)
558+
}
544559

545-
const serializedTransaction = await signTransaction(client, {
546-
...transaction,
547-
account,
548-
// @ts-expect-error
549-
feePayer: account,
550-
})
560+
if (
561+
request.method === 'eth_sendRawTransaction' ||
562+
request.method === 'eth_sendRawTransactionSync'
563+
) {
564+
const serialized = request.params?.[0] as `0x76${string}`
565+
const transaction = Transaction.deserialize(serialized)
566+
567+
const serializedTransaction = await signTransaction(client, {
568+
...transaction,
569+
account,
570+
// @ts-expect-error
571+
feePayer: account,
572+
})
573+
574+
const result = await client.request({
575+
method: request.method,
576+
params: [serializedTransaction],
577+
})
578+
579+
return Response.json(RpcResponse.from({ result }, { request }))
580+
}
551581

552582
return Response.json(
553-
RpcResponse.from({ result: serializedTransaction }, { request }),
583+
RpcResponse.from(
584+
{
585+
error: new RpcResponse.MethodNotSupportedError({
586+
message: `Method not supported: ${request.method}`,
587+
}),
588+
},
589+
{ request },
590+
),
591+
)
592+
} catch (error) {
593+
return Response.json(
594+
RpcResponse.from(
595+
{
596+
error: new RpcResponse.InternalError({
597+
message: (error as Error).message,
598+
}),
599+
},
600+
{ request },
601+
),
554602
)
555603
}
556-
557-
if (
558-
request.method === 'eth_sendRawTransaction' ||
559-
request.method === 'eth_sendRawTransactionSync'
560-
) {
561-
const serialized = request.params?.[0] as `0x76${string}`
562-
const transaction = Transaction.deserialize(serialized)
563-
564-
const serializedTransaction = await signTransaction(client, {
565-
...transaction,
566-
account,
567-
// @ts-expect-error
568-
feePayer: account,
569-
})
570-
571-
const result = await client.request({
572-
method: request.method,
573-
params: [serializedTransaction],
574-
})
575-
576-
return Response.json(RpcResponse.from({ result }, { request }))
577-
}
578-
579-
return Response.json(
580-
RpcResponse.from(
581-
{
582-
error: new RpcResponse.MethodNotSupportedError({
583-
message: `Method not supported: ${request.method}`,
584-
}),
585-
},
586-
{ request },
587-
),
588-
{ status: 400 },
589-
)
590604
})
591605

592606
return router

0 commit comments

Comments
 (0)