Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle HttpError on the client #8829

Merged
merged 4 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 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,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);
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
}

// 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