diff --git a/.changeset/mighty-keys-admire.md b/.changeset/mighty-keys-admire.md new file mode 100644 index 0000000..5af460d --- /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 41854ae..990147b 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 dcd6f8f..bffa20e 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