Skip to content

Commit 723eb8b

Browse files
fix: explain why json() cannot be used in form actions (#12829)
closes #12787 Adds a check to see if the data being returned is a Response object (such as with the helper method json()), then replies with a more helpful error message. --------- Co-authored-by: Ben McCann <[email protected]>
1 parent 312f130 commit 723eb8b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

.changeset/healthy-planets-dance.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: better error message when a `Result` is returned from a form action

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

+8
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ function try_deserialize(data, fn, route_id) {
280280
// If we're here, the data could not be serialized with devalue
281281
const error = /** @type {any} */ (e);
282282

283+
// if someone tries to use `json()` in their action
284+
if (data instanceof Response) {
285+
throw new Error(
286+
`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" });`
287+
);
288+
}
289+
290+
// if devalue could not serialize a property on the object, etc.
283291
if ('path' in error) {
284292
let message = `Data returned from action inside ${route_id} is not serializable: ${error.message}`;
285293
if (error.path !== '') message += ` (data.${error.path})`;

0 commit comments

Comments
 (0)