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] successfully handle client errors #2077

Merged
merged 1 commit into from
Aug 3, 2021
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/fast-cameras-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] successfully handle client errors
15 changes: 10 additions & 5 deletions packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { writable } from 'svelte/store';
import { hash } from '../hash.js';
import { normalize } from '../load.js';
import { coalesce_to_error } from '../utils.js';

/** @param {any} value */
function page_store(value) {
Expand Down Expand Up @@ -139,6 +140,8 @@ export class Renderer {
/** @type {import('./types').NavigationResult | undefined} */
let result;

let error_args;

try {
for (let i = 0; i < nodes.length; i += 1) {
const is_leaf = i === nodes.length - 1;
Expand All @@ -156,12 +159,12 @@ export class Renderer {
if (node && node.loaded) {
if (node.loaded.error) {
if (error) throw node.loaded.error;
result = await this._load_error({
error_args = {
status: node.loaded.status,
error: node.loaded.error,
path: page.path,
query: page.query
});
};
} else if (node.loaded.context) {
context = {
...context,
Expand All @@ -171,13 +174,15 @@ export class Renderer {
}
}

result = await this._get_navigation_result_from_branch({ page, branch });
} catch (e) {
result = error_args
? await this._load_error(error_args)
: await this._get_navigation_result_from_branch({ page, branch });
} catch (/** @type {unknown} */ e) {
if (error) throw e;

result = await this._load_error({
status: 500,
error: e,
error: coalesce_to_error(e),
path: page.path,
query: page.query
});
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { render_page } from './page/index.js';
import { render_response } from './page/render.js';
import { respond_with_error } from './page/respond_with_error.js';
import { parse_body } from './parse_body/index.js';
import { coalesce_to_error, lowercase_keys } from './utils.js';
import { lowercase_keys } from './utils.js';
import { coalesce_to_error } from '../utils.js';
import { hash } from '../hash.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/respond.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render_response } from './render.js';
import { load_node } from './load_node.js';
import { respond_with_error } from './respond_with_error.js';
import { coalesce_to_error } from '../utils.js';
import { coalesce_to_error } from '../../utils.js';

/**
* @typedef {import('./types.js').Loaded} Loaded
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/respond_with_error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render_response } from './render.js';
import { load_node } from './load_node.js';
import { coalesce_to_error } from '../utils.js';
import { coalesce_to_error } from '../../utils.js';

/**
* @param {{
Expand Down
8 changes: 0 additions & 8 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,3 @@ export function lowercase_keys(obj) {

return clone;
}

/**
* @param {unknown} err
* @return {Error}
*/
export function coalesce_to_error(err) {
return err instanceof Error ? err : new Error(JSON.stringify(err));
}
7 changes: 7 additions & 0 deletions packages/kit/src/runtime/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {unknown} err
* @return {Error}
*/
export function coalesce_to_error(err) {
return err instanceof Error ? err : new Error(JSON.stringify(err));
}