Skip to content

Conversation

@teemingc
Copy link
Member

@teemingc teemingc commented Aug 15, 2024

fixes #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
  • changeset

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • 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
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link

changeset-bot bot commented Aug 15, 2024

🦋 Changeset detected

Latest commit: ac9460b

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

@teemingc teemingc marked this pull request as draft August 15, 2024 15:00
@teemingc teemingc changed the title fix: add error result to preloadData fix: add error result type to preloadData Aug 15, 2024
@teemingc teemingc added needs-decision Not sure if we want to do this yet, also design work needed feature / enhancement New feature or request breaking change and removed needs-decision Not sure if we want to do this yet, also design work needed feature / enhancement New feature or request labels Oct 9, 2024
@teemingc teemingc added this to the 3.0 milestone Oct 9, 2024
@PavelNuzhin
Copy link

PavelNuzhin commented Nov 7, 2024

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():

image

Actually i think only problem is that get_status and get_message return default values

@PavelNuzhin
Copy link

PavelNuzhin commented Nov 7, 2024

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';
}

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-kit-12579-svelte.vercel.app/

this is an automated message

@teemingc
Copy link
Member Author

teemingc commented Nov 25, 2024

Hi @PavelNuzhin , can you provide a minimal reproduction along with an expected output compared to actual output?

@teemingc teemingc marked this pull request as ready for review November 25, 2024 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Navigation HTTP error handling

4 participants