0Pricing
Sveltejs Academy · Lesson

Error Handling in load

Throw error() or redirect() from load to handle failure cases cleanly.

Throwing Errors

Use error() from @sveltejs/kit to send a controlled HTTP error.

import { error } from "@sveltejs/kit";
if (!post) error(404, "Post not found");

Status Code

The first argument is the HTTP status code (404, 401, 500, etc.).

All lessons in this course

  1. +page.js load Function
  2. Returning Data and Using $page.data
  3. Error Handling in load
  4. +page.server.js for Server-Only Logic
← Back to Sveltejs Academy