fix: #1571 #1578 handle more error code

This commit is contained in:
Yidadaa 2023-05-18 00:14:29 +08:00
parent a402f646fe
commit 30676d118f

View File

@ -71,9 +71,13 @@ export class ChatGPTApi implements LLMApi {
if (shouldStream) { if (shouldStream) {
let responseText = ""; let responseText = "";
let finished = false;
const finish = () => { const finish = () => {
if (!finished) {
options.onFinish(responseText); options.onFinish(responseText);
finished = true;
}
}; };
controller.signal.onabort = finish; controller.signal.onabort = finish;
@ -83,19 +87,21 @@ export class ChatGPTApi implements LLMApi {
async onopen(res) { async onopen(res) {
clearTimeout(requestTimeoutId); clearTimeout(requestTimeoutId);
if ( if (
res.ok && !res.ok ||
res.headers.get("content-type") !== EventStreamContentType res.headers.get("content-type") !== EventStreamContentType ||
res.status !== 200
) { ) {
responseText += await res.clone().json();
return finish();
}
if (res.status === 401) {
let extraInfo = { error: undefined }; let extraInfo = { error: undefined };
try { try {
extraInfo = await res.clone().json(); extraInfo = await res.clone().json();
} catch {} } catch {}
responseText += "\n\n" + Locale.Error.Unauthorized; if (res.status === 401) {
if (responseText.length > 0) {
responseText += "\n\n";
}
responseText += Locale.Error.Unauthorized;
}
if (extraInfo.error) { if (extraInfo.error) {
responseText += "\n\n" + prettyObject(extraInfo); responseText += "\n\n" + prettyObject(extraInfo);