From 5b81a21e7bc3941dcd4184f73469bedd30440943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Bj=C3=B8rlig?= Date: Wed, 13 Dec 2023 10:18:05 +0100 Subject: [PATCH] fix: action not found should return 404 (#11278) When posting to a form action, that does exist, the server returned 500. With this fix, it will return 404 error "not found" --- .changeset/warm-sloths-cry.md | 5 +++++ .../kit/src/runtime/server/page/actions.js | 2 +- packages/kit/test/apps/basics/test/test.js | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 .changeset/warm-sloths-cry.md diff --git a/.changeset/warm-sloths-cry.md b/.changeset/warm-sloths-cry.md new file mode 100644 index 000000000000..14cbba832168 --- /dev/null +++ b/.changeset/warm-sloths-cry.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correctly return 404 when a form action is not found diff --git a/packages/kit/src/runtime/server/page/actions.js b/packages/kit/src/runtime/server/page/actions.js index 2c43d26be0a4..42b0e7d11b19 100644 --- a/packages/kit/src/runtime/server/page/actions.js +++ b/packages/kit/src/runtime/server/page/actions.js @@ -216,7 +216,7 @@ async function call_action(event, actions) { const action = actions[name]; if (!action) { - throw new Error(`No action with name '${name}' found`); + throw error(404, `No action with name '${name}' found`); } if (!is_form_content_type(event.request)) { diff --git a/packages/kit/test/apps/basics/test/test.js b/packages/kit/test/apps/basics/test/test.js index 89181ae423ab..6ad47289af0d 100644 --- a/packages/kit/test/apps/basics/test/test.js +++ b/packages/kit/test/apps/basics/test/test.js @@ -1260,6 +1260,24 @@ test.describe('Actions', () => { expect(error.message).toBe('Actions expect form-encoded data (received application/json)'); expect(response.status()).toBe(415); }); + + test('submitting to a form action that does not exists, should return http status code 404', async ({ + baseURL, + page + }) => { + const randomActionName = 'some-random-action'; + const response = await page.request.fetch(`${baseURL}/actions/enhance?/${randomActionName}`, { + method: 'POST', + body: 'irrelevant', + headers: { + Origin: `${baseURL}` + } + }); + const { type, error } = await response.json(); + expect(type).toBe('error'); + expect(error.message).toBe(`No action with name '${randomActionName}' found`); + expect(response.status()).toBe(404); + }); }); // Run in serial to not pollute the log with (correct) cookie warnings