Skip to content

Commit 172729c

Browse files
committed
chore: update the error handling logic
1 parent 90b777b commit 172729c

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/info.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"identifier": "customSystemPrompt",
8686
"type": "text",
8787
"title": "系统指令",
88+
"defaultValue": "You are a translation engine that can only translate text and cannot interpret it.",
8889
"desc": "可选项。自定义 System Prompt,填写则会覆盖默认的 System Prompt。自定义 Prompt可使用以下变量:\n\n`$text` - 需要翻译的文本,即翻译窗口输入框内的文本 `$sourceLang` - 原文语言,即翻译窗口输入框内文本的语言,比如「简体中文」\n\n`$targetLang` - 目标语言,即需要翻译成的语言,可以在翻译窗口中手动选择或自动检测,比如「English」",
8990
"textConfig": {
9091
"type": "visible",
@@ -101,6 +102,7 @@
101102
"identifier": "customUserPrompt",
102103
"type": "text",
103104
"title": "用户指令",
105+
"defaultValue": "translate from $sourceLang to $targetLang:\n\n$text",
104106
"desc": "可选项。自定义 User Prompt,填写则会覆盖默认的 User Prompt,默认值为`$text`(即翻译窗口输入框内的文本)。\n\n自定义 Prompt 中可以使用与系统指令中相同的变量",
105107
"textConfig": {
106108
"type": "visible",

src/main.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,49 @@ var ChatGPTModels = [
1212

1313
var SYSTEM_PROMPT = "You are a translation engine that can only translate text and cannot interpret it."
1414

15+
var HttpErrorCodes = {
16+
"400": "Bad Request",
17+
"401": "Unauthorized",
18+
"402": "Payment Required",
19+
"403": "Forbidden",
20+
"404": "Not Found",
21+
"405": "Method Not Allowed",
22+
"406": "Not Acceptable",
23+
"407": "Proxy Authentication Required",
24+
"408": "Request Timeout",
25+
"409": "Conflict",
26+
"410": "Gone",
27+
"411": "Length Required",
28+
"412": "Precondition Failed",
29+
"413": "Payload Too Large",
30+
"414": "URI Too Long",
31+
"415": "Unsupported Media Type",
32+
"416": "Range Not Satisfiable",
33+
"417": "Expectation Failed",
34+
"418": "I'm a teapot",
35+
"421": "Misdirected Request",
36+
"422": "Unprocessable Entity",
37+
"423": "Locked",
38+
"424": "Failed Dependency",
39+
"425": "Too Early",
40+
"426": "Upgrade Required",
41+
"428": "Precondition Required",
42+
"429": "Too Many Requests",
43+
"431": "Request Header Fields Too Large",
44+
"451": "Unavailable For Legal Reasons",
45+
"500": "Internal Server Error",
46+
"501": "Not Implemented",
47+
"502": "Bad Gateway",
48+
"503": "Service Unavailable",
49+
"504": "Gateway Timeout",
50+
"505": "HTTP Version Not Supported",
51+
"506": "Variant Also Negotiates",
52+
"507": "Insufficient Storage",
53+
"508": "Loop Detected",
54+
"510": "Not Extended",
55+
"511": "Network Authentication Required"
56+
}
57+
1558
/**
1659
* @param {string} url
1760
* @returns {string}
@@ -158,7 +201,6 @@ function buildRequestBody(model, isChatGPTModel, query) {
158201
};
159202
}
160203

161-
162204
/**
163205
* @param {Bob.TranslateQuery} query
164206
* @param {Bob.HttpResponse} result
@@ -170,13 +212,12 @@ function handleError(query, result) {
170212
query.onCompletion({
171213
error: {
172214
type: reason,
173-
message: `接口响应错误 - ${result.data.error.message}`,
174-
addtion: `${statusCode}: ${JSON.stringify(result)}`,
215+
message: `接口响应错误 - ${HttpErrorCodes[statusCode]}`,
216+
addtion: `${JSON.stringify(result)}`,
175217
},
176218
});
177219
}
178220

179-
180221
/**
181222
* @param {Bob.TranslateQuery} query
182223
* @param {boolean} isChatGPTModel

0 commit comments

Comments
 (0)