return detail error from api call. (#295)

This commit is contained in:
ryanhex53 2023-03-30 17:53:24 +08:00 committed by GitHub
parent 4d7adf477a
commit 3631a5ac75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 5 deletions

View File

@ -34,14 +34,15 @@ export const OpenAIStream = async (
}),
});
if (res.status !== 200) {
const statusText = res.statusText;
throw new Error(`OpenAI API returned an error: ${statusText}`);
}
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (res.status !== 200) {
const statusText = res.statusText;
const result = await res.body?.getReader().read();
throw new Error(`OpenAI API returned an error: ${decoder.decode(result?.value) || statusText}`);
}
const stream = new ReadableStream({
async start(controller) {
const onParse = (event: ParsedEvent | ReconnectInterval) => {