When using redirect()
from inside an async
function, the script crashes with this error.
Using import { redirect } from '@sveltejs/kit';
How can I redirect 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 Abstraction
// File 1 const my_http_request = async() =>{ return await http_request( method, url, data );};// File 2export async function http_request( method: HttpMethod, url: string ) { try { const response = await axios( { method, url, data } ); return response.data; } catch ( error: any ) { if ( error.response?.status === 403 ) show_error( 'Access Forbidden.' ); return false; }}// File 3const show_error = ( message: string ) => { redirect( 303, '/error?message='+ message );}