kit
kit copied to clipboard
fix: add error result type to `preloadData`
fixes https://github.com/sveltejs/kit/issues/12399
This PR changes the preloadData function so that it returns the 'error' type instead of 'loaded' when the page fails to load. It also returns the error that caused the loading to fail and the correct status (previously it was always 200).
EDIT: changed it to also return status for redirects
TODO: test and changeset
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.
Tests
- [x] Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- [ ] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
Edits
- [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
🦋 Changeset detected
Latest commit: ac9460b2df504a7bb00e982b36781af1ec14155f
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| @sveltejs/kit | Major |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
Hello! Thank you for that change!
I'm currently testing changes and noticed that the error I throw from the page is always returned as an Internal Error (500) from preloadData(). However, it seems logical that I would see, for example, my error(401, "Test error") from the load() function.
This is because when SvelteKit encounters my error in load_route and passes it to handle_error, this check happens:
if (error instanceof HttpError) {
return error.body;
}
The error I threw isn't recognized as an HttpError or a SvelteKitError. So the kit can't get its status and always returns Internal Error (500).
Here, for example, get_status function will never return my 401 status
export function get_status(error) {
return error instanceof HttpError || error instanceof SvelteKitError ? error.status : 500;
}
Is it intended behavior?
Here`s whats happening in handle_error():
Actually i think only problem is that get_status and get_message return default values
I fixed it for my project. It may be bad but it works for me :) I really appreciate it if you could make some changes to the preloadData() error handling.
kit/src/utils/error.js
/**
* @param {unknown} error
* @returns {number}
*/
export function get_status(error) {
if(error instanceof HttpError || error instanceof SvelteKitError)
return error.status;
if(error instanceof Object && 'status' in error && typeof error.status === 'number')
return error.status;
return 500;
}
/**
* @param {unknown} error
* @returns {string}
*/
export function get_message(error) {
if(error instanceof SvelteKitError)
return error.text;
if(error instanceof Object && 'error' in error)
error = error.error;
if(error instanceof Object && 'message' in error && typeof error.message === 'string')
return error.message;
return 'Internal Error';
}
preview: https://svelte-dev-git-preview-kit-12579-svelte.vercel.app/
this is an automated message
Hi @PavelNuzhin , can you provide a minimal reproduction along with an expected output compared to actual output?