kit icon indicating copy to clipboard operation
kit copied to clipboard

handleError is called for server-handled errors on the preload path, with status 500 instead of the node's status

Open RickCogley opened this issue 4 months ago • 3 comments

Describe the bug

When a link is preloaded (data-sveltekit-preload-data="hover", or preloadData()) and the target route's server load throws via error(404, …), the client handleError hook is invoked with the raw server error node and status: 500.

On a normal navigation this does not happen. The branch loader's catch checks server_data_nodes?.includes(err) and deliberately skips the client error handler, with a comment saying the error "should've already been handled on the server". The preload branch returns before that check is reached, so the guard never applies to it.

There are two distinct consequences:

  1. handleError receives a plain object { type: 'error', status, error } rather than an Error instance.
  2. The status argument is 500, not the node's actual status, because get_status() returns 500 for anything that is not an HttpError or SvelteKitError.

The second one is the more damaging of the two. Error-reporting integrations that filter out 4xx by looking at status cannot do so here — including @sentry/sveltekit, whose built-in is4xxError() reads the same argument. The net effect is that ordinary server-rendered 404s are reported as unhandled 500-level client exceptions, purely because a user hovered a link rather than clicking it. On a CMS-driven site, where 404s are a normal consequence of unpublished or locale-specific content, this is a steady stream of false alarms.

Reproduction

src/routes/missing/+page.server.ts

import { error } from '@sveltejs/kit';

export function load() {
  error(404, { message: 'nope' });
}

src/hooks.client.ts

export function handleError({ error, status }) {
  console.log('handleError', { status, error });
}

src/app.html — <body data-sveltekit-preload-data="hover">

Add a link to /missing on any page, then:

  • Hover the link. handleError fires:
    handleError { status: 500, error: { type: 'error', status: 404, error: { message: 'nope' } } }
    
  • Click the link instead, without hovering long enough to preload. handleError does not fire, and the 404 renders normally.

The same route, the same server error, reported differently depending on how the user's pointer approached the link.

Code references

packages/kit/src/runtime/client/client.js, in the branch loader's catch (line numbers from 2.61.1):

if (preload_tokens.has(preload)) {
  return preload_error({
    error: await handle_error(err, { params, url, route: { id: route.id } }),   // ~1306
    url, params, route
  });
}

let status = get_status(err);
let error;

if (server_data_nodes?.includes(err)) {
  // this is the server error rethrown above, reconstruct but don't invoke
  // the client error handler; it should've already been handled on the server
  status = err.status ?? status;                                               // ~1316
  error = err.error;
} else if (err instanceof HttpError) {
  error = err.body;
} else {
  error = await handle_error(err, { params, url, route: { id: route.id } });
}

The server_data_nodes guard at ~1316 expresses exactly the intent that the preload branch at ~1304 bypasses.

packages/kit/src/utils/error.js:

export function get_status(error) {
  return error instanceof HttpError || error instanceof SvelteKitError ? error.status : 500;
}

A rethrown server error node is neither, hence the 500.

Expected behaviour

Either would resolve it:

  1. Apply the same server_data_nodes check before the preload branch, so server-handled errors skip handle_error on both paths, matching the stated intent of the existing comment. This seems the most consistent with what the code already says it wants to do.
  2. If handleError should be called for preload failures, pass the node's own status rather than the 500 that get_status() infers, so status-based filtering behaves the same on both paths.

Severity

Not a correctness problem for users — the page still 404s properly, and preloading degrades gracefully. It is a reporting-fidelity problem, and the practical impact falls on anyone with error monitoring wired to handleError.

System

  • @sveltejs/kit 2.61.1
  • svelte 5.55.7
  • @sveltejs/adapter-cloudflare

Observed in production with @sentry/sveltekit 10.48.0, and reproduced against the versions above. Line numbers are from 2.61.1 and worth re-checking against main.

RickCogley avatar Jul 21 '26 07:07 RickCogley

This one is on version-3 too. In load_route the preload branch still runs first:

if (preload && preload_tokens.has(preload)) {
	throw await handle_error(err, { params, url, route: { id: route.id } });
}

so it goes through the client hook before the check just below it that recognises an error already handled on the server and skips handleError. Same ordering as on main.

I have the main fix in #16474 and I'm happy to port it to version-3 if that's useful.

ondraulehla avatar Jul 25 '26 17:07 ondraulehla

Above is incorrect. It as not an issue on version-3 since #12579: the server error node is rethrown as an HttpError, so the hook is skipped and the status is the real one. This only affects 2.x, so it can be closed unless a backport is wanted.

Nic-Polumeyv avatar Jul 26 '26 01:07 Nic-Polumeyv

Correction to my earlier comment: this is fixed on version-3. Since #12579 the server error node is rethrown as an HttpError, and handle_error returns error.body for HttpError before it ever reaches the hook, so handleError isn't called and the status comes from the node. I only looked at the ordering of the preload branch and missed that, sorry for the noise.

ondraulehla avatar Jul 26 '26 08:07 ondraulehla