Skip to content

fix: add error result type to preloadData - #12579

Merged
teemingc merged 20 commits into
version-3from
feat-preload-error
Jun 25, 2026
Merged

fix: add error result type to preloadData#12579
teemingc merged 20 commits into
version-3from
feat-preload-error

Conversation

@teemingc

@teemingc teemingc commented Aug 15, 2024

Copy link
Copy Markdown
Member

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

changeset-bot Bot commented Aug 15, 2024

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2e08d47

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

PavelNuzhin commented Nov 7, 2024

Copy link
Copy Markdown

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

PavelNuzhin commented Nov 7, 2024

Copy link
Copy Markdown

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

@teemingc

teemingc commented Nov 25, 2024

Copy link
Copy Markdown
Member Author

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
@svelte-docs-bot

Copy link
Copy Markdown

@teemingc
teemingc marked this pull request as draft February 5, 2026 18:08
@benmccann
benmccann changed the base branch from main to version-3 February 13, 2026 00:43
@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor

@teemingc want to bring this back up to date so we can review it? Is it ready?

Comment thread .changeset/modern-dodos-hang.md Outdated
@teemingc
teemingc marked this pull request as ready for review June 25, 2026 12:48
@teemingc

teemingc commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

Done! I've no idea how to reproduce the issue reported above though. Going to assume it was some bundling error that caused instanceof HttpError not to work.

EDIT: it was a server load that caused it because we were rethrowing a plain object rather than HttpError

Comment thread packages/kit/src/runtime/client/client.js

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@teemingc
teemingc merged commit 9ed38a8 into version-3 Jun 25, 2026
18 checks passed
@teemingc
teemingc deleted the feat-preload-error branch June 25, 2026 15:42
Rich-Harris pushed a commit that referenced this pull request Jun 26, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to version-3, this PR
will be updated.

⚠️⚠️⚠️⚠️⚠️⚠️

`version-3` is currently in **pre mode** so this branch has prereleases
rather than normal releases. If you want to exit prereleases, run
`changeset pre exit` on `version-3`.

⚠️⚠️⚠️⚠️⚠️⚠️

# Releases
## @sveltejs/adapter-netlify@7.0.0-next.1

### Major Changes


- breaking: require `vite@^8.0.12`, the first Vite 8 release bundling
stable `rolldown` 1.0.0
([#16134](#16134))


### Patch Changes

- Updated dependencies
[[`a9629f1`](a9629f1),
[`53d37f9`](53d37f9),
[`dba6239`](dba6239),
[`ab12cb6`](ab12cb6),
[`f0eab52`](f0eab52),
[`cd884c1`](cd884c1),
[`9ed38a8`](9ed38a8),
[`53aa049`](53aa049),
[`984e57a`](984e57a),
[`51785be`](51785be),
[`4f6bcbb`](4f6bcbb),
[`78e7137`](78e7137)]:
  - @sveltejs/kit@3.0.0-next.5
## @sveltejs/adapter-node@6.0.0-next.1

### Major Changes


- breaking: require `vite@^8.0.12`, the first Vite 8 release bundling
stable `rolldown` 1.0.0
([#16134](#16134))


### Patch Changes

- Updated dependencies
[[`a9629f1`](a9629f1),
[`53d37f9`](53d37f9),
[`dba6239`](dba6239),
[`ab12cb6`](ab12cb6),
[`f0eab52`](f0eab52),
[`cd884c1`](cd884c1),
[`9ed38a8`](9ed38a8),
[`53aa049`](53aa049),
[`984e57a`](984e57a),
[`51785be`](51785be),
[`4f6bcbb`](4f6bcbb),
[`78e7137`](78e7137)]:
  - @sveltejs/kit@3.0.0-next.5
## @sveltejs/adapter-vercel@7.0.0-next.1

### Major Changes


- breaking: require `vite@^8.0.12`, the first Vite 8 release bundling
stable `rolldown` 1.0.0
([#16134](#16134))


### Patch Changes

- Updated dependencies
[[`a9629f1`](a9629f1),
[`53d37f9`](53d37f9),
[`dba6239`](dba6239),
[`ab12cb6`](ab12cb6),
[`f0eab52`](f0eab52),
[`cd884c1`](cd884c1),
[`9ed38a8`](9ed38a8),
[`53aa049`](53aa049),
[`984e57a`](984e57a),
[`51785be`](51785be),
[`4f6bcbb`](4f6bcbb),
[`78e7137`](78e7137)]:
  - @sveltejs/kit@3.0.0-next.5
## @sveltejs/enhanced-img@1.0.0-next.1

### Major Changes


- breaking: require `vite@^8.0.12`, the first Vite 8 release bundling
stable `rolldown` 1.0.0
([#16134](#16134))
## @sveltejs/kit@3.0.0-next.5

### Major Changes


- breaking: remove `base`, `assets`, and `resolveRoute` from
`$app/paths` ([#15507](#15507))


- breaking: require Svelte config options to be passed through the Vite
plugin ([#16007](#16007))


- breaking: `goto` now rejects when called with a URL that does not
resolve to a route within the app, matching the existing behaviour for
external URLs ([#16164](#16164))


- breaking: add 'error' result type to `preloadData`
([#12579](#12579))


- breaking: require `vite@^8.0.12`, the first Vite 8 release bundling
stable `rolldown` 1.0.0
([#16134](#16134))


### Minor Changes


- feat: expose `submitted` property of remote forms
([#14811](#14811))


### Patch Changes


- fix: clear issues and touched states on form reset
([#16163](#16163))


- fix: return 404 for form actions and remote functions whose name is an
`Object.prototype` member
([#16072](#16072))


- feat: send periodic `keep-alive` SSE comments from `query.live` to
prevent idle-timeout errors
([#16063](#16063))


- fix: render pages over sibling endpoints without GET or HEAD handlers
([#16125](#16125))


- fix: sort directory entries when building the route manifest so node
indices are deterministic across runtimes (e.g. Bun and Node)
([#16074](#16074))


- fix: include hoisted packages in Vite's `server.fs.allow` list
([#15998](#15998))
## @sveltejs/package@3.0.0-next.1

### Patch Changes


- fix: declare typescript as an optional peer dependency so
svelte-package works under strict node-linkers
([#16073](#16073))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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

6 participants