From 26b19558478a87d3ae78bd65634a3f05c5df6798 Mon Sep 17 00:00:00 2001 From: Leboudin Date: Tue, 8 Aug 2023 11:17:53 +0800 Subject: [PATCH 1/3] handle error when fetch throws an exception --- cf-openai-azure-proxy.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cf-openai-azure-proxy.js b/cf-openai-azure-proxy.js index 51a266f..9d3679f 100644 --- a/cf-openai-azure-proxy.js +++ b/cf-openai-azure-proxy.js @@ -63,10 +63,18 @@ async function handleRequest(request) { body: typeof body === 'object' ? JSON.stringify(body) : '{}', }; - let response = await fetch(fetchAPI, payload); - response = new Response(response.body, response); - response.headers.set("Access-Control-Allow-Origin", "*"); - + let response + try { + response = await fetch(fetchAPI, payload); + response = new Response(response.body, response); + response.headers.set("Access-Control-Allow-Origin", "*"); + } catch (e) { + console.error(e.message); + response = new Response("An error occurred when processing your request, please try again later", { + status: 500 + }); + } + if (body?.stream != true){ return response } From e9f4f1d70bf67c1994c3d8b696cf148c40803112 Mon Sep 17 00:00:00 2001 From: Leboudin Date: Tue, 8 Aug 2023 11:49:52 +0800 Subject: [PATCH 2/3] Update cf-openai-azure-proxy.js Update error message --- cf-openai-azure-proxy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cf-openai-azure-proxy.js b/cf-openai-azure-proxy.js index 9d3679f..af9862c 100644 --- a/cf-openai-azure-proxy.js +++ b/cf-openai-azure-proxy.js @@ -69,8 +69,8 @@ async function handleRequest(request) { response = new Response(response.body, response); response.headers.set("Access-Control-Allow-Origin", "*"); } catch (e) { - console.error(e.message); - response = new Response("An error occurred when processing your request, please try again later", { + console.error(e) + response = new Response(`An error occurred when processing your request, please try again later. Error: ${e.message} ${response?.status}`, { status: 500 }); } From 8ea16f2e5434e7171fba127e9c4e9ccde13eb0a0 Mon Sep 17 00:00:00 2001 From: Leboudin Date: Tue, 8 Aug 2023 12:11:33 +0800 Subject: [PATCH 3/3] Update cf-openai-azure-proxy.js Update error message --- cf-openai-azure-proxy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cf-openai-azure-proxy.js b/cf-openai-azure-proxy.js index af9862c..ce51238 100644 --- a/cf-openai-azure-proxy.js +++ b/cf-openai-azure-proxy.js @@ -70,7 +70,7 @@ async function handleRequest(request) { response.headers.set("Access-Control-Allow-Origin", "*"); } catch (e) { console.error(e) - response = new Response(`An error occurred when processing your request, please try again later. Error: ${e.message} ${response?.status}`, { + response = new Response(`An error occurred when processing your request, please try again later. Error: ${e.message}`, { status: 500 }); }