Expected vs Unexpected Errors
Distinguish user-facing expected errors from unexpected server errors.
Expected vs Unexpected Errors is a free Sveltejs Academy lesson on CoddyKit — lesson 2 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Categories
SvelteKit distinguishes expected errors (thrown intentionally) from unexpected errors (bugs).
Expected: error()
Call error(status, message) for known failure cases.
if (!post) error(404, "Post not found");Unexpected: Uncaught Throws
Any other throw is unexpected. SvelteKit hides the message from clients for security.
Why Hide Messages
Unexpected error messages may leak stack details or implementation. Showing a generic one is safer.
handleError
Use handleError in hooks to log unexpected errors and customize the generic message.
Pattern: Validate Inputs
Call error() when inputs are invalid; let real bugs surface as unexpected.
Redirect Helper
redirect() is also intentional: use it for auth-required pages or moved URLs.
Try/Catch Recovery
Wrap risky external calls in try/catch and fall back gracefully, avoiding error pages.
TypeScript Types
SvelteKit's error() is typed; it throws, so the rest of your code is reachable after early return logic.
Status Code Choice
404 for not found, 401 for unauthorized, 403 for forbidden, 500 for server errors.
Avoid Throwing Plain Errors
Plain throw new Error in user-facing paths gets the unexpected treatment. Use error() for clear UX.
Quick Check
What does error(404) do differently from throw new Error?
Recap
Use error() for intentional failures with visible messages; let real bugs throw and be logged by handleError.
Frequently asked questions
Is the “Expected vs Unexpected Errors” lesson free?
Yes — the full text of “Expected vs Unexpected Errors” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. 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.
What will I learn in “Expected vs Unexpected Errors”?
Distinguish user-facing expected errors from unexpected server errors. 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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Expected vs Unexpected Errors” 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