Skip to content

Commit 40731fd

Browse files
committed
Implement streaming error handling
1 parent ebc2e99 commit 40731fd

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

packages/astro/src/server/middleware.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,18 @@ async function instrumentRequest(
184184

185185
const newResponseStream = new ReadableStream({
186186
start: async controller => {
187-
for await (const chunk of originalBody) {
188-
const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
189-
const modifiedHtml = addMetaTagToHead(html);
190-
controller.enqueue(new TextEncoder().encode(modifiedHtml));
187+
try {
188+
for await (const chunk of originalBody) {
189+
const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
190+
const modifiedHtml = addMetaTagToHead(html);
191+
controller.enqueue(new TextEncoder().encode(modifiedHtml));
192+
}
193+
} catch (e) {
194+
sendErrorToSentry(e);
195+
controller.error(e);
196+
} finally {
197+
controller.close();
191198
}
192-
controller.close();
193199
},
194200
});
195201

0 commit comments

Comments
 (0)