From 07dcc5df31476fb773a46d103136632e12762179 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Sun, 2 Apr 2023 11:51:21 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chatgpt-unofficial-proxy-api.ts | 1 - src/fetch-sse.ts | 32 ++++++++++++++++------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/chatgpt-unofficial-proxy-api.ts b/src/chatgpt-unofficial-proxy-api.ts index 2dd5b1e..57a3b0d 100644 --- a/src/chatgpt-unofficial-proxy-api.ts +++ b/src/chatgpt-unofficial-proxy-api.ts @@ -218,7 +218,6 @@ export class ChatGPTUnofficialProxyAPI { } } } catch (err) { - // ignore for now; there seem to be some non-json messages reject(err) } }, diff --git a/src/fetch-sse.ts b/src/fetch-sse.ts index e9ea479..08edacb 100644 --- a/src/fetch-sse.ts +++ b/src/fetch-sse.ts @@ -36,28 +36,32 @@ export async function fetchSSE( } }) - // check if the response is an error, if so, throw it + // handle special response errors const feed = (chunk: string) => { let response = null + try { response = JSON.parse(chunk) } catch { - /// ignore + // ignore } - if (response?.detail) { - if (response.detail.type === 'invalid_request_error') { - const msg = `ChatGPT error ${response.detail.message}: ${response.detail.code} (${response.detail.type})` - const error = new types.ChatGPTError(msg, { cause: response }) - error.statusCode = response.detail.code - error.statusText = response.detail.message - if (onError) { - onError(error) - } else { - console.error(error) - } - return // don't feed to event parser + + if (response?.detail?.type === 'invalid_request_error') { + const msg = `ChatGPT error ${response.detail.message}: ${response.detail.code} (${response.detail.type})` + const error = new types.ChatGPTError(msg, { cause: response }) + error.statusCode = response.detail.code + error.statusText = response.detail.message + + if (onError) { + onError(error) + } else { + console.error(error) } + + // don't feed to the event parser + return } + parser.feed(chunk) }