Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-kangaroos-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly handle HttpErrors on the client side
5 changes: 3 additions & 2 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1694,7 +1694,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
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -98,7 +97,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
});
Expand Down