Skip to content

Commit

Permalink
Add error handling to bailout from failure cases
Browse files Browse the repository at this point in the history
  • Loading branch information
smaeda-ks committed May 30, 2023
1 parent 17ae6ce commit 1bc49c5
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions utils/OpenAIStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ export async function OpenAIStream(payload: OpenAIStreamPayload) {
controller.enqueue(encoder.encode(data));
}
}

// optimistic error handling
if (res.status !== 200) {
const data = {
status: res.status,
statusText: res.statusText,
body: await res.text(),
}
console.log(`Error: recieved non-200 status code, ${JSON.stringify(data)}`);
controller.close();
return
}

// stream response (SSE) from OpenAI may be fragmented into multiple chunks
// this ensures we properly read chunks and invoke an event for each SSE event stream
const parser = createParser(onParse);
Expand Down

0 comments on commit 1bc49c5

Please sign in to comment.