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 };
}

Explanation

Expected errors created with error() show their message. Unexpected exceptions are hidden from users and go to handleError.

Further reading

Problem with a question

Svelte · Junior · question 55

Test yourself

Take the Svelte · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz