Skip to content
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

fix: add error result type to preloadData #12579

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino 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.

Copy link

changeset-bot bot commented Aug 15, 2024

⚠️ No Changeset found

Latest commit: 36b6542

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@eltigerchino eltigerchino marked this pull request as draft August 15, 2024 15:00
@eltigerchino eltigerchino changed the title fix: add error result to preloadData fix: add error result type to preloadData Aug 15, 2024
@eltigerchino eltigerchino added needs-decision Not sure if we want to do this yet, also design work needed feature request New feature or request breaking change and removed needs-decision Not sure if we want to do this yet, also design work needed feature request New feature or request labels Oct 9, 2024
@eltigerchino eltigerchino 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';
}

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
2 participants