Skip to content

Commit

Permalink
fix: handle stream errors
Browse files Browse the repository at this point in the history
Handles potential errors during stream reading in the HuggingFace service to prevent unhandled promise rejections.
  • Loading branch information
tak-bro committed Jul 18, 2024
1 parent 29dc7ed commit 0826464
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/services/ai/hugging-face.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,25 @@ export class HuggingFaceService extends AIService {

async function completeResponsePromise() {
// eslint-disable-next-line no-async-promise-executor
return new Promise<string>(async resolve => {
if (!modifiedStream) {
throw new Error(`ModifiedStream undefined`);
} else {
const reader = modifiedStream.getReader();

// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();

if (done) {
resolve(completeResponse);
break; // The streaming has ended.
return new Promise<string>(async (resolve, reject) => {
try {
if (!modifiedStream) {
reject(`ModifiedStream undefined`);
} else {
const reader = modifiedStream.getReader();

// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();

if (done) {
resolve(completeResponse);
break; // The streaming has ended.
}
}
}
} catch (error) {
reject(error); // Reject the promise with the caught error
}
});
}
Expand Down

0 comments on commit 0826464

Please sign in to comment.