Skip to content

Commit 951d8e6

Browse files
committed
handle errors in block expressions
1 parent b400780 commit 951d8e6

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @import { Effect, TemplateNode, Value } from '#client' */
22
import { DESTROYED } from '#client/constants';
3+
import { invoke_error_boundary } from '../../error-handling.js';
34
import { async_derived } from '../../reactivity/deriveds.js';
45
import { active_effect } from '../../runtime.js';
56
import { capture, get_pending_boundary } from './boundary.js';
@@ -9,7 +10,7 @@ import { capture, get_pending_boundary } from './boundary.js';
910
* @param {Array<() => Promise<any>>} expressions
1011
* @param {(anchor: TemplateNode, ...deriveds: Value[]) => void} fn
1112
*/
12-
export function async(node, expressions, fn) {
13+
export async function async(node, expressions, fn) {
1314
// TODO handle hydration
1415

1516
var parent = /** @type {Effect} */ (active_effect);
@@ -19,12 +20,16 @@ export function async(node, expressions, fn) {
1920

2021
boundary.increment();
2122

22-
Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => {
23+
try {
24+
const result = await Promise.all(expressions.map((fn) => async_derived(fn)));
25+
2326
if ((parent.f & DESTROYED) !== 0) return;
2427

2528
restore();
2629
fn(node, ...result);
27-
30+
} catch (error) {
31+
invoke_error_boundary(error, parent);
32+
} finally {
2833
boundary.decrement();
29-
});
34+
}
3035
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { tick } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target }) {
6+
assert.htmlEqual(target.innerHTML, 'loading');
7+
await tick();
8+
assert.htmlEqual(target.innerHTML, 'nope');
9+
}
10+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<svelte:boundary>
2+
{#if await Promise.reject(new Error('nope'))}
3+
hi
4+
{/if}
5+
6+
{#snippet pending()}loading{/snippet}
7+
{#snippet failed(e)}{e.message}{/snippet}
8+
</svelte:boundary>

0 commit comments

Comments
 (0)