Skip to content

Commit e56a13c

Browse files
fix: correctly handle HttpError on the client (#8829)
Closes #7966 --------- Co-authored-by: Simon H <[email protected]>
1 parent 64a279f commit e56a13c

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

Diff for: .changeset/fuzzy-kangaroos-relate.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: correctly handle HttpErrors on the client side

Diff for: packages/kit/src/runtime/client/client.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ export function create_client({ target }) {
745745
server_data = await load_data(url, invalid_server_nodes);
746746
} catch (error) {
747747
return load_root_error_page({
748-
status: 500,
748+
status: error instanceof HttpError ? error.status : 500,
749749
error: await handle_error(error, { url, params, route: { id: route.id } }),
750750
url,
751751
route
@@ -1693,7 +1693,8 @@ async function load_data(url, invalid) {
16931693

16941694
if (!res.ok) {
16951695
// error message is a JSON-stringified string which devalue can't handle at the top level
1696-
throw new Error(data);
1696+
// turn it into a HttpError to not call handleError on the client again (was already handled on the server)
1697+
throw new HttpError(res.status, data);
16971698
}
16981699

16991700
// revive devalue-flattened data

Diff for: packages/kit/src/runtime/server/utils.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as devalue from 'devalue';
22
import { json, text } from '../../exports/index.js';
33
import { coalesce_to_error } from '../../utils/error.js';
44
import { negotiate } from '../../utils/http.js';
5-
import { has_data_suffix } from '../../utils/url.js';
65
import { HttpError } from '../control.js';
76
import { fix_stack_trace } from '../shared.js';
87

@@ -81,7 +80,7 @@ export async function handle_fatal_error(event, options, error) {
8180
'text/html'
8281
]);
8382

84-
if (has_data_suffix(new URL(event.request.url).pathname) || type === 'application/json') {
83+
if (event.isDataRequest || type === 'application/json') {
8584
return json(body, {
8685
status
8786
});

0 commit comments

Comments
 (0)