From 8e1b4edc9134875bd9ddcfaa31d926c088dcc5f0 Mon Sep 17 00:00:00 2001 From: ChenZhaoYu <790348264@qq.com> Date: Tue, 7 Mar 2023 16:59:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BD=AC=E4=B9=89=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/src/chatgpt/index.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/service/src/chatgpt/index.ts b/service/src/chatgpt/index.ts index a0bd5ef..78f7098 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -7,6 +7,14 @@ import fetch from 'node-fetch' import { sendResponse } from '../utils' import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types' +const ErrorCodeMessage: Record = { + 401: '提供错误的API密钥 | Incorrect API key provided', + 429: '服务器限流,请稍后再试 | Server was limited, please try again later', + 503: '服务器繁忙,请稍后再试 | Server is busy, please try again later', + 500: '服务器繁忙,请稍后再试 | Server is busy, please try again later', + 403: '服务器拒绝访问,请稍后再试 | Server refused to access, please try again later', +} + dotenv.config() const timeoutMs: number = !isNaN(+process.env.TIMEOUT_MS) ? +process.env.TIMEOUT_MS : 30 * 1000 @@ -98,8 +106,10 @@ async function chatReplyProcess( return sendResponse({ type: 'Success', data: response }) } catch (error: any) { - global.console.error(error) - return sendResponse({ type: 'Fail', message: error.message }) + const code = error.statusCode || 'unknown' + if (Reflect.has(ErrorCodeMessage, code)) + return sendResponse({ type: 'Fail', message: ErrorCodeMessage[code] }) + return sendResponse({ type: 'Fail', message: `${error.statusCode}-${error.statusText}` }) } }