From 7bbaedd6c749809ba987d937a57c6cd7c0fbd71b Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Fri, 25 Oct 2024 20:38:25 +0100 Subject: [PATCH] fix: rethrow errors from await block if no catch block exists (#13819) * chore: highlight swallowed errors from await blocks in DEV * chore: highlight swallowed errors from await blocks in DEV * chore: highlight swallowed errors from await blocks in DEV * lint * feedback * feedback * add test * Update packages/svelte/tests/runtime-runes/samples/await-no-catch-error/main.svelte --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --- .changeset/blue-boats-call.md | 5 +++++ .../src/internal/client/dom/blocks/await.js | 4 ++++ .../samples/await-no-catch-error/_config.js | 22 +++++++++++++++++++ .../samples/await-no-catch-error/main.svelte | 10 +++++++++ 4 files changed, 41 insertions(+) create mode 100644 .changeset/blue-boats-call.md create mode 100644 packages/svelte/tests/runtime-runes/samples/await-no-catch-error/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/await-no-catch-error/main.svelte diff --git a/.changeset/blue-boats-call.md b/.changeset/blue-boats-call.md new file mode 100644 index 0000000000..0f6c31025d --- /dev/null +++ b/.changeset/blue-boats-call.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: rethrow errors from await block if no catch block exists diff --git a/packages/svelte/src/internal/client/dom/blocks/await.js b/packages/svelte/src/internal/client/dom/blocks/await.js index 75bbb23cdd..3c6bc9900d 100644 --- a/packages/svelte/src/internal/client/dom/blocks/await.js +++ b/packages/svelte/src/internal/client/dom/blocks/await.js @@ -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; + } } ); diff --git a/packages/svelte/tests/runtime-runes/samples/await-no-catch-error/_config.js b/packages/svelte/tests/runtime-runes/samples/await-no-catch-error/_config.js new file mode 100644 index 0000000000..568b58ff95 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/await-no-catch-error/_config.js @@ -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/); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/await-no-catch-error/main.svelte b/packages/svelte/tests/runtime-runes/samples/await-no-catch-error/main.svelte new file mode 100644 index 0000000000..bd2fb1e2f2 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/await-no-catch-error/main.svelte @@ -0,0 +1,10 @@ + + + + +{#if toggle} + {#await promise}{/await} +{/if}