From e6c11803a567474fb329e5b25beee90d39781906 Mon Sep 17 00:00:00 2001 From: Vitor Date: Tue, 3 Dec 2024 10:20:24 -0300 Subject: [PATCH] chore: add error handling for bundler api --- apps/popup/app/api/bundler/[chainId]/route.ts | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/apps/popup/app/api/bundler/[chainId]/route.ts b/apps/popup/app/api/bundler/[chainId]/route.ts index 4896c40c..10889b74 100644 --- a/apps/popup/app/api/bundler/[chainId]/route.ts +++ b/apps/popup/app/api/bundler/[chainId]/route.ts @@ -5,27 +5,32 @@ export async function POST( request: Request, { params: { chainId: chainIdStr } }: { params: { chainId: string } }, ) { - const chainId = Number(chainIdStr); - if (!isValidChain(chainId)) { - return new Response('Chain not supported', { status: 404 }); - } + try { + const chainId = Number(chainIdStr); + if (!isValidChain(chainId)) { + return new Response('Chain not supported', { status: 404 }); + } - const body = await request.json(); + const body = await request.json(); - const response = await fetch( - `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${env.NEXT_PUBLIC_PIMLICO_API_KEY}`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', + const response = await fetch( + `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${env.NEXT_PUBLIC_PIMLICO_API_KEY}`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), }, - body: JSON.stringify(body), - }, - ); + ); - if (!response.ok) { - return new Response(response.statusText, { status: response.status }); - } + if (!response.ok) { + return new Response(response.statusText, { status: response.status }); + } - return new Response(JSON.stringify(await response.json()), { status: 200 }); + return new Response(JSON.stringify(await response.json()), { status: 200 }); + } catch (error) { + console.log('Error processing request', error); + return new Response('Error processing request', { status: 500 }); + } }