Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Apr 2, 2023
1 parent c0c2e71 commit 07dcc5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/chatgpt-unofficial-proxy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ export class ChatGPTUnofficialProxyAPI {
}
}
} catch (err) {
// ignore for now; there seem to be some non-json messages
reject(err)
}
},
Expand Down
32 changes: 18 additions & 14 deletions src/fetch-sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 07dcc5d

Please sign in to comment.