Skip to content

Commit

Permalink
prevent crash on stream error (from PR 9533)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Jan 8, 2024
1 parent 4658177 commit 91d1d81
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ export class NodeApp extends App {
try {
const reader = body.getReader();
destination.on('close', () => {
reader.cancel();
// Cancelling the reader may reject not just because of
// an error in the ReadableStream's cancel callback, but
// also because of an error anywhere in the stream.
reader.cancel().catch(err => {
console.error(`There was an uncaught error in the middle of the stream while rendering ${destination.req.url}.`, err);
});
});
let result = await reader.read();
while (!result.done) {
destination.write(result.value);
result = await reader.read();
}
} catch (err: any) {
console.error(err?.stack || err?.message || String(err));
// the error will be logged by the "on end" callback above
} catch {
destination.write('Internal server error');
}
}
Expand Down

0 comments on commit 91d1d81

Please sign in to comment.