This function runs while rendering. The button changes the data; the error is thrown on the next render, not in the event handler.
A boundary with a failed snippet catches rendering errors inside it and removes the failed content.
Fix the data before calling reset. It recreates the boundary's contents; retrying with the same broken data would fail again.
<script>
let task = $state('Walk dog');
function renderTask(value) {
if (!value) throw new Error('This todo needs a task.');
return value;
}
</script>
<h1>Todo List</h1>
<button onclick={() => task = ''}>Try a broken todo</button>
<svelte:boundary>
<p>{renderTask(task)}</p>
{#snippet failed(error, reset)}
<p>Couldn't show this todo: {error.message}</p>
<button onclick={() => {
task = 'Walk dog';
reset();
}}>Restore and retry</button>
{/snippet}
</svelte:boundary>