error() and redirect() Helpers
Throw typed errors and redirects from load functions and actions.
error() and redirect() Helpers is a free Sveltejs Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.
Two Built-In Helpers
error() and redirect() from @sveltejs/kit handle the two main user-facing branches.
error Helper
Throws a structured error with status and message.
import { error } from "@sveltejs/kit";
error(404, "Not found");redirect Helper
Sends the user to another URL with a 3xx status.
import { redirect } from "@sveltejs/kit";
redirect(303, "/login");Status Codes for Redirect
301 for permanent, 302 for temporary, 303 for "see other" (common after POST), 307/308 for method-preserving.
Use in load
Call inside load to redirect before render.
if (!session) redirect(303, "/login");Use in actions
Form actions can redirect after successful processing.
Throws Pattern
Both helpers throw; subsequent code is unreachable. TypeScript narrows correctly.
Never Catch Them
Do not wrap these in try/catch; let SvelteKit handle them.
Status 401 vs 403
Use 401 when unauthenticated; 403 when authenticated but not allowed.
Combine with URLs
Build URLs with the URL constructor for safe encoding.
redirect(303, `/login?next=${encodeURIComponent(url.pathname)}`);Real Use
Auth flows, deprecated URL redirects, missing-resource handling.
Quick Check
Why use redirect() instead of return?
Recap
Use error() for failures and redirect() for navigation. Both throw and stop further code.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 41
- Lessons
- 163
Frequently Asked Questions
Is the “error() and redirect() Helpers” lesson free?
Yes — the full text of “error() and redirect() Helpers” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO. The Sveltejs Academy course includes 4 lessons in total.
What will I learn in “error() and redirect() Helpers”?
Throw typed errors and redirects from load functions and actions. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “error() and redirect() Helpers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- +error.svelte for Custom Error Pages
- Expected vs Unexpected Errors
- error() and redirect() Helpers
- Error Pages in Nested Layouts