-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[breaking] error handling rework #6586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
Defining: is throwing error and forcing me to rename the file to: What is the point of server: setting? |
Is there a reason why you would not want to name your files like so?: const config = {
// ...
files: {
hooks: {
server: 'src/hooks/index.server',
client: 'src/hooks/index.client',
},
}
}I think the compiler needs the |
|
I only use server hooks and my configuration points to it. Either use configuration or convention, but here, we are mixing both. |
|
if compiler depend on the naming, lets drop using "server:" and "client:" and use array only: |
|
This might just be a bug related to us trying to be helpful showing an error Message regarding the breaking change. I have to look more closely into it. |
1. Remove methodOverride from svelte.conf.js Ref: sveltejs/kit#6469 2. Rename hooks.js to hooks.server.js Ref: sveltejs/kit#6586 3. Change post not found status code from 400 to 404 4. Remove useless import { page } in +layout.svelte
|
If handle() is not implemented in hooks.client.js ... how can I prevent the user navigating between pages? I used to check there if the user had a cookie, if not then redirect to login. How it's suppouse to do this now? or best practice? |
How to migrate
Step 1: Rename hooks file
There's now also a hooks file for the client.
kit.files.hooksis therefore now{ server: string; client: string }instead ofstring. So if you have used that setting, you need to adjust your config:// svelte.config.js export default { kit: { - hooks: 'src/myhooks' + hooks: { server: 'src/myhooks' }If you didn't use that setting, then by default the server hooks now live in
src/hooks.server, so you need to rename the file:Step 2: Adjust types
The interface
HandleErrorhas been renamed toHandleServerError, to disambiguate it from the new interfaceHandleClientError. The error type has been changed tounknown. Previously it was typedError, but we can't know if this is always the case, so we align with TypeScript's type incatchhere. This likely means you need to do a type cast, either by checking if it indeed is anError, or just trusting your (third party) code to always throwErrors:Step 3: Define the desired error shape
The shape of
$page.erroris now defined by thehandleErrorhook, which exists on the server and the client.$page.erroris of typeApp.PageError, which you can define yourself inapp.d.ts:You then need to make sure that the
handleErrorfunction in bothhooks.server.jsandhooks.client.jsadhere to the type by returning that shape from the function:If
handleErroris not defined or the return value is undefined, then SvelteKit falls back to{ message: 'Internal Error' }PR description
Implements #6499
handleErrorreturns JSONApp.PageDatatypehandleErrorclient hookPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. All changesets should bepatchuntil SvelteKit 1.0