From 91ebc2d1d1226f056da9fd2d1d9abd985981a687 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 18 Oct 2024 11:27:00 +0800 Subject: [PATCH 1/7] add better error message --- packages/kit/src/runtime/server/page/actions.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/kit/src/runtime/server/page/actions.js b/packages/kit/src/runtime/server/page/actions.js index 64db810507a8..c53774df12f8 100644 --- a/packages/kit/src/runtime/server/page/actions.js +++ b/packages/kit/src/runtime/server/page/actions.js @@ -280,6 +280,13 @@ function try_deserialize(data, fn, route_id) { // If we're here, the data could not be serialized with devalue const error = /** @type {any} */ (e); + // if someone tries to use `json()` in their action + if (data instanceof Response) { + throw new Error( + `Data returned from action inside ${route_id} is not serializable. Form actions need to return objects directly or fail(). e.g. return { success: true } or return fail(400, { message: "invalid" });` + ); + } + if ('path' in error) { let message = `Data returned from action inside ${route_id} is not serializable: ${error.message}`; if (error.path !== '') message += ` (data.${error.path})`; From bf60632021065813dd21eee3a61f54c78bf953a0 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 18 Oct 2024 11:28:08 +0800 Subject: [PATCH 2/7] changeset --- .changeset/healthy-planets-dance.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/healthy-planets-dance.md diff --git a/.changeset/healthy-planets-dance.md b/.changeset/healthy-planets-dance.md new file mode 100644 index 000000000000..31d3c9c59bf7 --- /dev/null +++ b/.changeset/healthy-planets-dance.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: explain why `json()` cannot be used in form actions From d279ff5291f4aa8be039bcb2e8e642a73fba58d3 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 18 Oct 2024 11:31:09 +0800 Subject: [PATCH 3/7] plain objects --- packages/kit/src/runtime/server/page/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/page/actions.js b/packages/kit/src/runtime/server/page/actions.js index c53774df12f8..1c018b3ed927 100644 --- a/packages/kit/src/runtime/server/page/actions.js +++ b/packages/kit/src/runtime/server/page/actions.js @@ -283,7 +283,7 @@ function try_deserialize(data, fn, route_id) { // if someone tries to use `json()` in their action if (data instanceof Response) { throw new Error( - `Data returned from action inside ${route_id} is not serializable. Form actions need to return objects directly or fail(). e.g. return { success: true } or return fail(400, { message: "invalid" });` + `Data returned from action inside ${route_id} is not serializable. Form actions need to return plain objects or fail(). e.g. return { success: true } or return fail(400, { message: "invalid" });` ); } From f62eaebc4e74524acb4145add2e8de545d1ee9c5 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Fri, 18 Oct 2024 11:41:46 +0800 Subject: [PATCH 4/7] Update .changeset/healthy-planets-dance.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- .changeset/healthy-planets-dance.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/healthy-planets-dance.md b/.changeset/healthy-planets-dance.md index 31d3c9c59bf7..aaeda9d953b3 100644 --- a/.changeset/healthy-planets-dance.md +++ b/.changeset/healthy-planets-dance.md @@ -2,4 +2,4 @@ '@sveltejs/kit': patch --- -fix: explain why `json()` cannot be used in form actions +fix: better error message when a `Result` is returned from a form action From 8116883b01eae4d5154cc4294feeff07916157ce Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Fri, 18 Oct 2024 12:35:52 +0800 Subject: [PATCH 5/7] Update packages/kit/src/runtime/server/page/actions.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- packages/kit/src/runtime/server/page/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/page/actions.js b/packages/kit/src/runtime/server/page/actions.js index 1c018b3ed927..a69156a1acde 100644 --- a/packages/kit/src/runtime/server/page/actions.js +++ b/packages/kit/src/runtime/server/page/actions.js @@ -283,7 +283,7 @@ function try_deserialize(data, fn, route_id) { // if someone tries to use `json()` in their action if (data instanceof Response) { throw new Error( - `Data returned from action inside ${route_id} is not serializable. Form actions need to return plain objects or fail(). e.g. return { success: true } or return fail(400, { message: "invalid" });` + `Data returned from action inside ${route_id} is not serializable. Form actions need to return plain objects or fail(). E.g. return { success: true } or return fail(400, { message: "invalid" });` ); } From 4be8f452ca320088f7b9bd91dc90211af9ae46fe Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 18 Oct 2024 12:38:06 +0800 Subject: [PATCH 6/7] add comment for devalue error branch --- packages/kit/src/runtime/server/page/actions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/kit/src/runtime/server/page/actions.js b/packages/kit/src/runtime/server/page/actions.js index a69156a1acde..9de69e585d45 100644 --- a/packages/kit/src/runtime/server/page/actions.js +++ b/packages/kit/src/runtime/server/page/actions.js @@ -287,6 +287,7 @@ function try_deserialize(data, fn, route_id) { ); } + // if devalue could not serialize the data if ('path' in error) { let message = `Data returned from action inside ${route_id} is not serializable: ${error.message}`; if (error.path !== '') message += ` (data.${error.path})`; From b8e8cc7b191d437a39e246fdd551e44696754b8c Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 18 Oct 2024 12:39:11 +0800 Subject: [PATCH 7/7] more specifically --- packages/kit/src/runtime/server/page/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/page/actions.js b/packages/kit/src/runtime/server/page/actions.js index 9de69e585d45..3fa3f895baa4 100644 --- a/packages/kit/src/runtime/server/page/actions.js +++ b/packages/kit/src/runtime/server/page/actions.js @@ -287,7 +287,7 @@ function try_deserialize(data, fn, route_id) { ); } - // if devalue could not serialize the data + // if devalue could not serialize a property on the object, etc. if ('path' in error) { let message = `Data returned from action inside ${route_id} is not serializable: ${error.message}`; if (error.path !== '') message += ` (data.${error.path})`;