Skip to content

Commit

Permalink
fix: correctly handle HttpError on the client (#8829)
Browse files Browse the repository at this point in the history
Closes #7966

---------

Co-authored-by: Simon H <[email protected]>
  • Loading branch information
gtm-nayan and dummdidumm authored Feb 3, 2023
1 parent 64a279f commit e56a13c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
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 @@ -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
Expand Down Expand Up @@ -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
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 @@ -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
});
Expand Down

0 comments on commit e56a13c

Please sign in to comment.