If you have a sveltekit project configured in SPA mode: export const ssr = false;
in the +layout.ts
, how do you handle an exception being thrown from the <script>
tags:
<!-- src/routes/test/+page.svelte -->
<script lang="ts">
throw new Error('hey');
</script>
If you have a sveltekit project configured in SPA mode: export const ssr = false;
in the +layout.ts
, how do you handle an exception being thrown from the <script>
tags:
<!-- src/routes/test/+page.svelte -->
<script lang="ts">
throw new Error('hey');
</script>
Share
Improve this question
asked Jan 18 at 11:55
Robert MihaiRobert Mihai
3743 silver badges12 bronze badges
2 Answers
Reset to default 1I am not aware of a way to catch that in a +layout.ts
. There is a handleError
hook, but that would be more global and does not seem to capture errors thrown during initialization.
Without SSR, a boundary could be used in the +layout.svelte
to show an error, though:
<svelte:boundary>
{@render children()}
{#snippet failed(error, reset)}
<p>Something went wrong</p>
<button onclick={reset}>Retry</button>
{/snippet}
</svelte:boundary>
(Requires at least Svelte 5.3.0)
You can listen for uncaught errors by listen for the error event on the window
object.
window.addEventListener("error", (event) => {
log.textContent = `${log.textContent}${event.type}: ${event.message}\n`
console.log(event)
})
But perhaps the other provided answer is more suitable for your specific case.