Svelte · Junior · Question 55 of 76
How do you show a 404 page from load when a post doesn't exist?
import { error } from "@sveltejs/kit";
export async function load({ params }) {
const post = await db.getPost(params.slug);
if (!post) error(404, "Not found");
return { post };
}- ✓Correct answer: Call
error(404, ...)from@sveltejs/kit; SvelteKit renders the nearest+error.svelte - 2Return
{ status: 404 } - 3Throw
new Error("404")
Explanation
Expected errors created with error() show their message. Unexpected exceptions are hidden from users and go to handleError.