-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[breaking] error handling rework #6586
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.hooks
is therefore now{ server: string; client: string }
instead ofstring
. So if you have used that setting, you need to adjust your config: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
HandleError
has 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 incatch
here. 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 throwError
s:Step 3: Define the desired error shape
The shape of
$page.error
is now defined by thehandleError
hook, which exists on the server and the client.$page.error
is of typeApp.PageError
, which you can define yourself inapp.d.ts
:You then need to make sure that the
handleError
function in bothhooks.server.js
andhooks.client.js
adhere to the type by returning that shape from the function:If
handleError
is not defined or the return value is undefined, then SvelteKit falls back to{ message: 'Internal Error' }
PR description
Implements #6499
handleError
returns JSONApp.PageData
typehandleError
client hookPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0