diff --git a/.changeset/fuzzy-kangaroos-relate.md b/.changeset/fuzzy-kangaroos-relate.md new file mode 100644 index 000000000000..0c27b68dd624 --- /dev/null +++ b/.changeset/fuzzy-kangaroos-relate.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correctly handle HttpErrors on the client side diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 85f979e900a3..49b034ab30b9 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -745,7 +745,7 @@ export function create_client({ target }) { server_data = await load_data(url, invalid_server_nodes); } catch (error) { return load_root_error_page({ - status: 500, + status: error instanceof HttpError ? error.status : 500, error: await handle_error(error, { url, params, route: { id: route.id } }), url, route @@ -1693,7 +1693,8 @@ async function load_data(url, invalid) { if (!res.ok) { // error message is a JSON-stringified string which devalue can't handle at the top level - throw new Error(data); + // turn it into a HttpError to not call handleError on the client again (was already handled on the server) + throw new HttpError(res.status, data); } // revive devalue-flattened data diff --git a/packages/kit/src/runtime/server/utils.js b/packages/kit/src/runtime/server/utils.js index bb9bb65dfd07..ed8c4803d4c0 100644 --- a/packages/kit/src/runtime/server/utils.js +++ b/packages/kit/src/runtime/server/utils.js @@ -2,7 +2,6 @@ import * as devalue from 'devalue'; import { json, text } from '../../exports/index.js'; import { coalesce_to_error } from '../../utils/error.js'; import { negotiate } from '../../utils/http.js'; -import { has_data_suffix } from '../../utils/url.js'; import { HttpError } from '../control.js'; import { fix_stack_trace } from '../shared.js'; @@ -81,7 +80,7 @@ export async function handle_fatal_error(event, options, error) { 'text/html' ]); - if (has_data_suffix(new URL(event.request.url).pathname) || type === 'application/json') { + if (event.isDataRequest || type === 'application/json') { return json(body, { status });