Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: suspended error not re-thrown #336

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-spoons-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix error thrown after suspending not being rethrown.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ function _renderToString(
try {
return renderChildren();
} catch (e) {
if (!e || typeof e.then !== 'function') throw e;

return e.then(
() => renderChildren(),
() => renderNestedChildren()
Expand Down
28 changes: 28 additions & 0 deletions test/compat/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,32 @@ describe('Async renderToString', () => {

expect(rendered).to.equal(expected);
});

it('should rethrow error thrown after suspending', async () => {
const { suspended, getResolved } = createSuspender();

function Suspender() {
if (!getResolved()) {
throw suspended.promise;
}

throw new Error('fail');
}

const promise = renderToStringAsync(
<Suspense fallback={<div>loading...</div>}>
<Suspender />
</Suspense>
);

let msg = '';
try {
suspended.resolve();
await promise;
} catch (err) {
msg = err.message;
}

expect(msg).to.equal('fail');
});
});
3 changes: 3 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function createSuspender() {
}

return {
getResolved() {
return resolved;
},
suspended: deferred,
Suspender
};
Expand Down
Loading