From 30676d118f4b6e699472c07b8ca1609202fd7535 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Thu, 18 May 2023 00:14:29 +0800 Subject: [PATCH] fix: #1571 #1578 handle more error code --- app/client/platforms/openai.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index b6ea9750..c2dbbb4f 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -71,9 +71,13 @@ export class ChatGPTApi implements LLMApi { if (shouldStream) { let responseText = ""; + let finished = false; const finish = () => { - options.onFinish(responseText); + if (!finished) { + options.onFinish(responseText); + finished = true; + } }; controller.signal.onabort = finish; @@ -83,19 +87,21 @@ export class ChatGPTApi implements LLMApi { async onopen(res) { clearTimeout(requestTimeoutId); if ( - res.ok && - res.headers.get("content-type") !== EventStreamContentType + !res.ok || + res.headers.get("content-type") !== EventStreamContentType || + res.status !== 200 ) { - responseText += await res.clone().json(); - return finish(); - } - if (res.status === 401) { let extraInfo = { error: undefined }; try { extraInfo = await res.clone().json(); } 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) { responseText += "\n\n" + prettyObject(extraInfo);