From e56a13c68a7d885f83f519c43f3510f10fee4ca5 Mon Sep 17 00:00:00 2001 From: gtmnayan <50981692+gtm-nayan@users.noreply.github.com> Date: Fri, 3 Feb 2023 17:12:40 +0545 Subject: [PATCH] fix: correctly handle HttpError on the client (#8829) Closes #7966 --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --- .changeset/fuzzy-kangaroos-relate.md | 5 +++++ packages/kit/src/runtime/client/client.js | 5 +++-- packages/kit/src/runtime/server/utils.js | 3 +-- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 .changeset/fuzzy-kangaroos-relate.md 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 });