Skip to content

fix: rethrow errors from await block if no catch block exists #13819

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

Merged
merged 8 commits into from
Oct 25, 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/blue-boats-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: rethrow errors from await block if no catch block exists
4 changes: 4 additions & 0 deletions packages/svelte/src/internal/client/dom/blocks/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
// but let's use internal_set for consistency and just to be safe
internal_set(error_source, error);
update(CATCH, true);
if (!catch_fn) {
// Rethrow the error if no catch block exists
throw error_source.v;
}
}
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
const b1 = target.querySelector('button');

let err = '';
window.addEventListener('error', (e) => {
e.preventDefault();
err = e.message;
});

b1?.click();
await Promise.resolve();
flushSync();

assert.throws(() => {
throw err;
}, /Test/);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
const promise = Promise.reject('Test');
let toggle = $state(false);
</script>

<button onclick={() => toggle = !toggle}>toggle</button>

{#if toggle}
{#await promise}{/await}
{/if}