Skip to content

Commit 091c950

Browse files
dummdidummRich-Harrisgeoffrich
authored
[fix] more informative serialization error messages (#7303)
* [fix] more informative serialization error messages * Update packages/kit/src/runtime/server/page/actions.js Co-authored-by: Geoff Rich <[email protected]> * Update packages/kit/src/runtime/server/page/actions.js * fix message, apply to client-side as well, update tests Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Geoff Rich <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 7875003 commit 091c950

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

.changeset/serious-llamas-smoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[fix] more informative serialization error messages

packages/kit/src/runtime/server/data/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export async function render_data(event, route, options, state) {
111111
nodes: nodes.slice(0, length)
112112
};
113113

114-
return data_response(server_data);
114+
return data_response(server_data, event);
115115
} catch (e) {
116116
const error = normalize_error(e);
117117

@@ -122,10 +122,10 @@ export async function render_data(event, route, options, state) {
122122
location: error.location
123123
};
124124

125-
return data_response(server_data);
125+
return data_response(server_data, event);
126126
} else {
127127
// TODO make it clearer that this was an unexpected error
128-
return data_response(handle_error_and_jsonify(event, options, error));
128+
return data_response(handle_error_and_jsonify(event, options, error), event);
129129
}
130130
}
131131
}

packages/kit/src/runtime/server/page/actions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,9 @@ function check_serializability(value, id, path) {
248248
}
249249
}
250250

251-
throw new Error(`${path} returned from action in ${id} cannot be serialized as JSON`);
251+
throw new Error(
252+
`${path} returned from action in ${id} cannot be serialized as JSON without losing its original type` +
253+
// probably the most common case, so let's give a hint
254+
(value instanceof Date ? ' (Date objects are serialized as strings)' : '')
255+
);
252256
}

packages/kit/src/runtime/server/page/render.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ export async function render_response({
177177
// function, but it would mean passing more stuff around than we currently do
178178
const error = /** @type {any} */ (e);
179179
const match = /\[(\d+)\]\.data\.(.+)/.exec(error.path);
180-
if (match) throw new Error(`${error.message} (data.${match[2]})`);
180+
if (match) {
181+
throw new Error(
182+
`Data returned from \`load\` while rendering /${event.routeId} is not serializable: ${error.message} (data.${match[2]})`
183+
);
184+
}
181185
throw error;
182186
}
183187

packages/kit/src/runtime/server/utils.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ export function allowed_methods(mod) {
6767
return allowed;
6868
}
6969

70-
/** @param {any} data */
71-
export function data_response(data) {
70+
/**
71+
* @param {any} data
72+
* @param {import('types').RequestEvent} event
73+
*/
74+
export function data_response(data, event) {
7275
const headers = {
7376
'content-type': 'application/json',
7477
'cache-control': 'private, no-store'
@@ -79,7 +82,9 @@ export function data_response(data) {
7982
} catch (e) {
8083
const error = /** @type {any} */ (e);
8184
const match = /\[(\d+)\]\.data\.(.+)/.exec(error.path);
82-
const message = match ? `${error.message} (data.${match[2]})` : error.message;
85+
const message = match
86+
? `Data returned from \`load\` while rendering /${event.routeId} is not serializable: ${error.message} (data.${match[2]})`
87+
: error.message;
8388
return new Response(JSON.stringify(message), { headers, status: 500 });
8489
}
8590
}

packages/kit/test/apps/basics/test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ test.describe('Shadowed pages', () => {
308308

309309
expect(await page.textContent('h1')).toBe('500');
310310
expect(await page.textContent('#message')).toBe(
311-
'This is your custom error page saying: "Cannot stringify arbitrary non-POJOs (data.nope)"'
311+
'This is your custom error page saying: "Data returned from `load` while rendering /shadowed/serialization is not serializable: Cannot stringify arbitrary non-POJOs (data.nope)"'
312312
);
313313
});
314314
}

0 commit comments

Comments
 (0)