Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

How to call error() from within an async function in Svelte?

$
0
0

When using error() from inside an async function, the script crashes with this error.

Using import { redirect } from '@sveltejs/kit';

How can I call error() from within an async function in Svelte?

node:internal/process/promises:289            triggerUncaughtException(err, true /* fromPromise */);            ^[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<Redirect>".] {  code: 'ERR_UNHANDLED_REJECTION'}Node.js v20.11.0

Code Minimum Samplesrc/routes/+page.svelte (loading base URL)

<script lang="ts">    import { error } from '@sveltejs/kit';    const tst = async() => error( 400, 'My error message' );    tst();</script>

Case 2 This code produces a smilar error (uncaught (in promise)) in the JS console

<script lang="ts">    import { error } from '@sveltejs/kit';    import { onMount } from 'svelte';    onMount( async function()     {        error( 400, 'My error message' );    } ); </script>

Screenshot

enter image description here

Removing async from the function does solves this issue, but I would like to use async please.

And I also would like to avoid having to use try .. catch around every async function call.


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>