Skip to content

Commit

Permalink
Merge pull request #370 from Chanzhaoyu/feature
Browse files Browse the repository at this point in the history
feat: 转义状态码
  • Loading branch information
Chanzhaoyu authored Mar 7, 2023
2 parents dea74e2 + 8e1b4ed commit 2ff82b1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import fetch from 'node-fetch'
import { sendResponse } from '../utils'
import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types'

const ErrorCodeMessage: Record<string, string> = {
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
Expand Down Expand Up @@ -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}` })
}
}

Expand Down

0 comments on commit 2ff82b1

Please sign in to comment.