UnhandledPromiseRejection - sveltekit - +page.server.ts - load function

540 Views Asked by At

i add a load function in server side of sveltekit like this (+page.server.ts):

export const load: PageServerLoad = async ({ locals }) => {
    const { data, error } = await locals.sb.auth.getSession()
    if(data.session) {
        throw redirect(303, '/dashboard')
    } else {
        throw redirect(303,'/login')
    }
};

But get this errors:

[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'
}

How can i fix this?

i have tried search but there is result using .catch() but i'm not sure how to use in svetekit?

1

There are 1 best solutions below

0
Nicola Biada On

Use an empty try/catch, like this:

try {
    throw redirect(303, '/login')
} catch {       
}