Skip to content

Commit 31f829d

Browse files
authored
[fix] error cleanup rework (#6675)
- add missing changeset, closes #6664 - enhance docs, closes #6669 - fix fallback message in client
1 parent 842f69b commit 31f829d

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.changeset/long-icons-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[breaking] hooks file renames; error shape defined through handleError

documentation/docs/07-hooks.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ If an unexpected error is thrown during loading or rendering, this function will
140140
- you can log the error
141141
- you can generate a custom representation of the error that is safe to show to users, omitting sensitive details like messages and stack traces. The returned value, which defaults to `{ message: 'Internal Error' }`, becomes the value of `$page.error`. To make this type-safe, you can customize the expected shape by declaring an `App.PageError` interface (which must include `message: string`, to guarantee sensible fallback behavior).
142142

143+
The following code shows an example of typing the error shape as `{ message: string; code: string }` and returning it accordingly from the `handleError` functions:
144+
145+
```ts
146+
/// file: src/app.d.ts
147+
declare namespace App {
148+
interface PageError {
149+
message: string;
150+
code: string;
151+
}
152+
}
153+
```
154+
143155
```js
144156
/// file: src/hooks.server.js
145157
// @errors: 2322 2571
@@ -160,8 +172,28 @@ export function handleError({ error, event }) {
160172
}
161173
```
162174
175+
```js
176+
/// file: src/hooks.client.js
177+
// @errors: 2322 2571
178+
// @filename: ambient.d.ts
179+
const Sentry: any;
180+
181+
// @filename: index.js
182+
// ---cut---
183+
/** @type {import('@sveltejs/kit').HandleClientError} */
184+
export function handleError({ error, event }) {
185+
// example integration with https://sentry.io/
186+
Sentry.captureException(error, { event });
187+
188+
return {
189+
message: 'Whoops!',
190+
code: error.code ?? 'UNKNOWN'
191+
};
192+
}
193+
```
194+
163195
> In `src/hooks.client.js`, the type of `handleError` is `HandleClientError` instead of `HandleServerError`, and `event` is a `NavigationEvent` rather than a `RequestEvent`.
164196
165197
This function is not called for _expected_ errors (those thrown with the [`error`](/docs/modules#sveltejs-kit-error) function imported from `@sveltejs/kit`).
166198
167-
During development, if an error occurs because of a syntax error in your Svelte code, a `frame` property will be appended highlighting the location of the error.
199+
During development, if an error occurs because of a syntax error in your Svelte code, the passed in error has a `frame` property appended highlighting the location of the error.

packages/kit/src/runtime/client/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ async function load_data(url, invalid) {
15061506
* @returns {App.PageError}
15071507
*/
15081508
function handle_error(error, event) {
1509-
return hooks.handleError({ error, event }) ?? /** @type {any} */ ({ error: 'Internal Error' });
1509+
return hooks.handleError({ error, event }) ?? /** @type {any} */ ({ message: 'Internal Error' });
15101510
}
15111511

15121512
// TODO remove for 1.0

0 commit comments

Comments
 (0)