From 9746d5f8a416bf5edcb4c6be0893c012bca00f1f Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Wed, 1 Feb 2023 16:31:24 +0545 Subject: [PATCH 1/4] fix: correctly handle `HttpError` on the client --- packages/kit/src/runtime/client/client.js | 4 ++-- packages/kit/src/runtime/server/utils.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 0369e27102dc..5699259683ab 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -746,7 +746,7 @@ export function create_client({ target, base }) { 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 @@ -1694,7 +1694,7 @@ 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); + 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 49f873e41375..7d0f49380ecb 100644 --- a/packages/kit/src/runtime/server/utils.js +++ b/packages/kit/src/runtime/server/utils.js @@ -98,7 +98,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 (has_data_suffix(event.url.pathname) || type === 'application/json') { return json(body, { status }); From f8ca097f1e69fadd12f9c539c102b41c32edc575 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Wed, 1 Feb 2023 16:32:16 +0545 Subject: [PATCH 2/4] changeset --- .changeset/fuzzy-kangaroos-relate.md | 5 +++++ 1 file changed, 5 insertions(+) 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 From 8abbb5b7503033e7109efb851f844cbe069df31e Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 3 Feb 2023 11:57:53 +0100 Subject: [PATCH 3/4] simplify --- packages/kit/src/runtime/server/utils.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/kit/src/runtime/server/utils.js b/packages/kit/src/runtime/server/utils.js index 7d0f49380ecb..fb4b4789c2be 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'; @@ -98,7 +97,7 @@ export async function handle_fatal_error(event, options, error) { 'text/html' ]); - if (has_data_suffix(event.url.pathname) || type === 'application/json') { + if (event.isDataRequest || type === 'application/json') { return json(body, { status }); From 8c411eb30a0bc47fa5cc979f45bae547e9d9899a Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 3 Feb 2023 12:21:06 +0100 Subject: [PATCH 4/4] Update packages/kit/src/runtime/client/client.js --- packages/kit/src/runtime/client/client.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 5699259683ab..68f2051c4c5e 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -1694,6 +1694,7 @@ 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 + // 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); }