Skip to main content
Svelte基础
介绍
响应
属性Props
逻辑表达式
事件
绑定
Classes和样式
动作Actions
转场
Svelte高阶
响应式进阶
内容复用
动画
高级绑定
高级转场
上下文API
特殊元素
脚本模块
接下来
SvelteKit基础
介绍
路由
加载数据
Headers和cookies
Shared modules
Forms
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Hooks
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:

src/routes/expected/+page.server
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:

src/routes/unexpected/+page.server
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.

Edit this page on GitHub

previous next
1
2
<h1>home</h1>