'Returnning the error page in Hooks / Sveltekit

The documentation seems a bit vague about showing the error page from Hooks. I am looking for a way to show the 404 page conditionally.

export async function handle({ event, resolve }) {
  return new Response('This page does not exist.', {status: 404})
}

This returns a status 404 page in a plain text, but I want it to return an error page with a layout, not just text. Is there any API to use? Or should I redirect to the 404 page?



Solution 1:[1]

To use the error page within hooks you can throw an uncaught exception.

throw new Exception(exception.status, exception.message);

This will show the necessary error page with your layout.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob