You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* breaking: tighten up error handling
Introduces a NonFatalError object that is used internally and that is user-detectable in handleError
* how did this happen
* fix tests
* lint
* types
* adjust wording (is this even a breaking change now?)
* adjust
* pass status and message to handleError
* Apply suggestions from code review
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
* lint
* Update documentation/docs/30-advanced/20-hooks.md
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
* lint
* simplify example
* tweak docs
* Update documentation/docs/30-advanced/20-hooks.md
* various tweaks. we can be less duplicative i think
* tweak
* tweak
* handle too large body after streaming has started
* cancel stream from the inside if content-length exceeds limit
* remove unnecessary try-catch, bump adapter-node/adapter-vercel majors
* migration docs
* tweak/fix tests
* fix more
* more
---------
Co-authored-by: Rich Harris <rich.harris@vercel.com>
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Copy file name to clipboardExpand all lines: documentation/docs/30-advanced/20-hooks.md
+15-8Lines changed: 15 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,12 +139,14 @@ The following can be added to `src/hooks.server.js` _and_ `src/hooks.client.js`:
139
139
140
140
### handleError
141
141
142
-
If an unexpected error is thrown during loading or rendering, this function will be called with the `error` and the `event`. This allows for two things:
142
+
If an [unexpected error](/docs/errors#unexpected-errors) is thrown during loading or rendering, this function will be called with the `error`, `event`, `status` code and `message`. This allows for two things:
143
143
144
144
- you can log the error
145
-
- 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 becomes the value of `$page.error`. It defaults to `{ message: 'Not Found' }` in case of a 404 (you can detect them through `event.route.id` being `null`) and to `{ message: 'Internal Error' }` for everything else. To make this type-safe, you can customize the expected shape by declaring an `App.Error` interface (which must include `message: string`, to guarantee sensible fallback behavior).
145
+
- 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 }`, becomes the value of `$page.error`.
146
146
147
-
The following code shows an example of typing the error shape as `{ message: string; errorId: string }` and returning it accordingly from the `handleError` functions:
147
+
For errors thrown from your code (or library code called by your code) the status will be 500 and the message will be "Internal Error". While `error.message` may contain sensitive information that should not be exposed to users, `message` is safe (albeit meaningless to the average user).
148
+
149
+
To add more information to the `$page.error` object in a type-safe way, you can customize the expected shape by declaring an `App.Error` interface (which must include `message: string`, to guarantee sensible fallback behavior). This allows you to — for example — append a tracking ID for users to quote in correspondence with your technical support staff:
Copy file name to clipboardExpand all lines: documentation/docs/30-advanced/25-errors.md
+1-30Lines changed: 1 addition & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,36 +77,7 @@ By default, unexpected errors are printed to the console (or, in production, you
77
77
{ "message": "Internal Error" }
78
78
```
79
79
80
-
Unexpected errors will go through the [`handleError`](hooks#shared-hooks-handleerror) hook, where you can add your own error handling — for example, sending errors to a reporting service, or returning a custom error object.
> Make sure that `handleError` _never_ throws an error
80
+
Unexpected errors will go through the [`handleError`](hooks#shared-hooks-handleerror) hook, where you can add your own error handling — for example, sending errors to a reporting service, or returning a custom error object which becomes `$page.error`.
Copy file name to clipboardExpand all lines: documentation/docs/60-appendix/30-migrating-to-sveltekit-2.md
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,6 +105,12 @@ As such, SvelteKit 2 replaces `resolvePath` with a (slightly better named) funct
105
105
106
106
`svelte-migrate` will do the method replacement for you, though if you later prepend the result with `base`, you need to remove that yourself.
107
107
108
+
## Improved error handling
109
+
110
+
Errors are handled inconsistently in SvelteKit 1. Some errors trigger the `handleError` hook but there is no good way to discern their status (for example, the only way to tell a 404 from a 500 is by seeing if `event.route.id` is `null`), while others (such as 405 errors for `POST` requests to pages without actions) don't trigger `handleError` at all, but should. In the latter case, the resulting `$page.error` will deviate from the [`App.Error`](/docs/types#app-error) type, if it is specified.
111
+
112
+
SvelteKit 2 cleans this up by calling `handleError` hooks with two new properties: `status` and `message`. For errors thrown from your code (or library code called by your code) the status will be `500` and the message will be `Internal Error`. While `error.message` may contain sensitive information that should not be exposed to users, `message` is safe.
113
+
108
114
## Dynamic environment variables cannot be used during prerendering
109
115
110
116
The `$env/dynamic/public` and `$env/dynamic/private` modules provide access to _run time_ environment variables, as opposed to the _build time_ environment variables exposed by `$env/static/public` and `$env/static/private`.
@@ -127,6 +133,12 @@ If a form contains an `<input type="file">` but does not have an `enctype="multi
127
133
128
134
Previously, the generated `tsconfig.json` was trying its best to still produce a somewhat valid config when your `tsconfig.json` included `paths` or `baseUrl`. In SvelteKit 2, the validation is more strict and will warn when you use either `paths` or `baseUrl` in your `tsconfig.json`. These settings are used to generate path aliases and you should use [the `alias` config](configuration#alias) option in your `svelte.config.js` instead, to also create a corresponding alias for the bundler.
129
135
136
+
## `getRequest` no longer throws errors
137
+
138
+
The `@sveltejs/kit/node` module exports helper functions for use in Node environments, including `getRequest` which turns a Node [`ClientRequest`](https://nodejs.org/api/http.html#class-httpclientrequest) into a standard [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object.
139
+
140
+
In SvelteKit 1, `getRequest` could throw if the `Content-Length` header exceeded the specified size limit. In SvelteKit 2, the error will not be thrown until later, when the request body (if any) is being read. This enables better diagnostics and simpler code.
141
+
130
142
## `vitePreprocess` is no longer exported from `@sveltejs/kit/vite`
131
143
132
144
Since `@sveltejs/vite-plugin-svelte` is now a peer dependency, SvelteKit 2 no longer re-exports `vitePreprocess`. You should import it directly from `@svelte/vite-plugin-svelte`.
0 commit comments