Skip to content

Commit

Permalink
fix: ChatGPTNextWeb#1571 ChatGPTNextWeb#1578 handle more error code
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed May 17, 2023
1 parent a402f64 commit 30676d1
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 30676d1

Please sign in to comment.