From f3476d8d918bc951ec112f33077d4480dea51064 Mon Sep 17 00:00:00 2001 From: Tianzhou Chen Date: Tue, 2 May 2023 01:46:06 +0800 Subject: [PATCH] chore: fix error message and readable message for openai key missing --- src/components/ConversationView/index.tsx | 3 +-- src/pages/api/chat.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/components/ConversationView/index.tsx b/src/components/ConversationView/index.tsx index dd5f1c9..76da933 100644 --- a/src/components/ConversationView/index.tsx +++ b/src/components/ConversationView/index.tsx @@ -218,8 +218,7 @@ const ConversationView = () => { let errorMessage = "Failed to request message, please check your network."; try { - const res = await rawRes.json(); - errorMessage = res.error.message; + errorMessage = await rawRes.statusText; } catch (error) { // do nth } diff --git a/src/pages/api/chat.ts b/src/pages/api/chat.ts index 4eb892e..e557e38 100644 --- a/src/pages/api/chat.ts +++ b/src/pages/api/chat.ts @@ -30,6 +30,15 @@ const handler = async (req: NextRequest) => { const reqBody = await req.json(); const openAIApiConfig = reqBody.openAIApiConfig; const apiKey = openAIApiConfig?.key || openAIApiKey; + + if (!apiKey) { + return new Response("Unauthorized", { + status: 401, + statusText: + "OpenAI API Key is missing. You can supply your own key via Settings.", + }); + } + const apiEndpoint = getApiEndpoint( openAIApiConfig?.endpoint || openAIApiEndpoint );