Browse Source

fixup: add more error info

Yidadaa 1 year ago
parent
commit
71cbf86b2c
3 changed files with 16 additions and 6 deletions
  1. 2 3
      app/api/auth.ts
  2. 13 0
      app/client/platforms/openai.ts
  3. 1 3
      app/store/chat.ts

+ 2 - 3
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 {

+ 13 - 0
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();
             }
           },

+ 1 - 3
app/store/chat.ts

@@ -296,9 +296,7 @@ export const useChatStore = create<ChatStore>()(
               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;