From 310e1bb067ee93776e805eddb737ebe51a266408 Mon Sep 17 00:00:00 2001 From: f0x Date: Sat, 4 Jan 2025 20:20:18 +0100 Subject: [PATCH] renderToPipeableStream: expose errors through onError, no longer emit un-catchable error event on internal stream --- .changeset/mighty-keys-admire.md | 5 +++++ src/stream-node.d.ts | 2 +- src/stream-node.js | 17 ++++++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 .changeset/mighty-keys-admire.md diff --git a/.changeset/mighty-keys-admire.md b/.changeset/mighty-keys-admire.md new file mode 100644 index 00000000..5af460dc --- /dev/null +++ b/.changeset/mighty-keys-admire.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': patch +--- + +renderToPipeableStream: expose errors through onError, no longer emit un-catchable error event on internal stream diff --git a/src/stream-node.d.ts b/src/stream-node.d.ts index 5cdc0e53..75e815a8 100644 --- a/src/stream-node.d.ts +++ b/src/stream-node.d.ts @@ -8,7 +8,7 @@ interface RenderToPipeableStreamOptions { } interface PipeableStream { - abort: () => void; + abort: (reason?: unknown) => void; pipe: (writable: WritableStream) => void; } diff --git a/src/stream-node.js b/src/stream-node.js index dcd6f8f1..bffa20e0 100644 --- a/src/stream-node.js +++ b/src/stream-node.js @@ -44,7 +44,12 @@ export function renderToPipeableStream(vnode, options, context) { stream.end(); }) .catch((error) => { - stream.destroy(error); + stream.destroy(); + if (options.onError) { + options.onError(error); + } else { + throw error; + } }); Promise.resolve().then(() => { @@ -52,9 +57,15 @@ export function renderToPipeableStream(vnode, options, context) { }); return { - abort() { + /** + * @param {unknown} [reason] + */ + abort(reason = new Error('The render was aborted by the server without a reason.')) { controller.abort(); - stream.destroy(new Error('aborted')); + stream.destroy(); + if (options.onError) { + options.onError(reason); + } }, /** * @param {import("stream").Writable} writable