From 71cbf86b2c8e21407045ba848ba38237134b837d Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Tue, 16 May 2023 01:58:58 +0800 Subject: [PATCH] fixup: add more error info --- app/api/auth.ts | 5 ++--- app/client/platforms/openai.ts | 13 +++++++++++++ app/store/chat.ts | 4 +--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/api/auth.ts b/app/api/auth.ts index 1005c5ff..62fcd226 100644 --- a/app/api/auth.ts +++ b/app/api/auth.ts @@ -43,8 +43,7 @@ export function auth(req: NextRequest) { if (serverConfig.needCode && !serverConfig.codes.has(hashedCode) && !token) { return { error: true, - needAccessCode: true, - msg: "Please go settings page and fill your access code.", + msg: !accessCode ? "empty access code" : "wrong access code", }; } @@ -58,7 +57,7 @@ export function auth(req: NextRequest) { console.log("[Auth] admin did not provide an api key"); return { error: true, - msg: "Empty Api Key", + msg: "admin did not provide an api key", }; } } else { diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index cc1ecb91..99f36520 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -4,6 +4,7 @@ import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; import { ChatOptions, getHeaders, LLMApi, LLMUsage } from "../api"; import Locale from "../../locales"; import { fetchEventSource } from "@microsoft/fetch-event-source"; +import { prettyObject } from "@/app/utils/format"; export class ChatGPTApi implements LLMApi { public ChatPath = "v1/chat/completions"; @@ -72,12 +73,24 @@ export class ChatGPTApi implements LLMApi { options.onFinish(responseText); }; + controller.signal.onabort = finish; + fetchEventSource(chatPath, { ...chatPayload, async onopen(res) { clearTimeout(reqestTimeoutId); if (res.status === 401) { + let extraInfo = { error: undefined }; + try { + extraInfo = await res.clone().json(); + } catch {} + responseText += "\n\n" + Locale.Error.Unauthorized; + + if (extraInfo.error) { + responseText += "\n\n" + prettyObject(extraInfo); + } + return finish(); } }, diff --git a/app/store/chat.ts b/app/store/chat.ts index 9257d263..9bb9a803 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -296,9 +296,7 @@ export const useChatStore = create()( botMessage.content !== Locale.Error.Unauthorized && !isAborted ) { - botMessage.content += "\n\n" + Locale.Store.Error; - } else if (botMessage.content.length === 0) { - botMessage.content = prettyObject(error); + botMessage.content += "\n\n" + prettyObject(error); } botMessage.streaming = false; userMessage.isError = !isAborted;