From 2fda5538d4c2e86ad43c38ccb35a1ee34d86c1bb Mon Sep 17 00:00:00 2001 From: timonson Date: Tue, 21 May 2024 05:14:21 +0200 Subject: [PATCH] Catch error --- server/response.ts | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/server/response.ts b/server/response.ts index 3882c9b..7abfec5 100644 --- a/server/response.ts +++ b/server/response.ts @@ -93,25 +93,29 @@ export function respondByHandlingFormData( return async (request: Request): Promise => { const headers = options.headers ?? new Headers(); if (request.headers.get("content-type")?.includes("multipart/form-data")) { - const formData = await request.formData(); - const rpcRequestString = formData.get("rpc")?.toString(); - if (rpcRequestString) { - const newRequest = createNewRpcRequest(request, rpcRequestString); - const blobs: Record = {}; - formData.forEach((value, key) => { - if (key !== "rpc") { - blobs[key] = value as Blob; - } - }); - const updatedOptions = { - ...options, - acceptFormData: false, - args: { ...options.args, blobs }, - }; - return await respond(methods, updatedOptions, authInput)( - newRequest, - ); - } else { + try { + const formData = await request.formData(); + const rpcRequestString = formData.get("rpc")?.toString(); + if (rpcRequestString) { + const newRequest = createNewRpcRequest(request, rpcRequestString); + const blobs: Record = {}; + formData.forEach((value, key) => { + if (key !== "rpc") { + blobs[key] = value as Blob; + } + }); + const updatedOptions = { + ...options, + acceptFormData: false, + args: { ...options.args, blobs }, + }; + return await respond(methods, updatedOptions, authInput)( + newRequest, + ); + } else { + throw new Error(); + } + } catch { headers.append("content-type", "application/json"); return new Response( JSON.stringify({