Basic Svelte
Introduction
Bindings
Classes and styles
Advanced Svelte
Advanced reactivity
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion
There are two types of errors in SvelteKit β expected errors and unexpected errors.
An expected error is one that was thrown via the error
helper from @sveltejs/kit
, as in src/routes/expected/+page.server.js
:
import { error } from '@sveltejs/kit';
export function load() {
error(420, 'Enhance your calm');
}
Any other error β such as the one in src/routes/unexpected/+page.server.js
β is treated as unexpected:
export function load() {
throw new Error('Kaboom!');
}
When you throw an expected error, youβre telling SvelteKit βdonβt worry, I know what Iβm doing hereβ. An unexpected error, by contrast, is assumed to be a bug in your app. When an unexpected error is thrown, its message and stack trace will be logged to the console.
In a later chapter weβll learn about how to add custom error handling using the
handleError
hook.
If you click the links in this app, youβll notice an important difference: the expected error message is shown to the user, whereas the unexpected error message is redacted and replaced with a generic βInternal Errorβ message and a 500 status code. Thatβs because error messages can contain sensitive data.
<h1>home</h1>