From 7a5e1d482810ad639f8438df1f6e363d4d5f5bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Bj=C3=B8rlig?= Date: Tue, 12 Dec 2023 23:44:06 +0100 Subject: [PATCH 1/2] fix: action not found should return 404 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 | 21 +++++++++++++++++++ 3 files changed, 27 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 cd43bb06e428..8e706997f681 100644 --- a/packages/kit/test/apps/basics/test/test.js +++ b/packages/kit/test/apps/basics/test/test.js @@ -1238,6 +1238,27 @@ 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 body = new FormData(); + body.append('foo', 'bar'); + + const response = await page.request.fetch(`${baseURL}/actions/enhance?/${randomActionName}`, { + method: 'POST', + body, + 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 From 56a28f047e543d7b97612d4ead6dd86e358696b8 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 13 Dec 2023 09:49:32 +0100 Subject: [PATCH 2/2] Update packages/kit/test/apps/basics/test/test.js --- packages/kit/test/apps/basics/test/test.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/kit/test/apps/basics/test/test.js b/packages/kit/test/apps/basics/test/test.js index 8e706997f681..85335f1ce1d1 100644 --- a/packages/kit/test/apps/basics/test/test.js +++ b/packages/kit/test/apps/basics/test/test.js @@ -1238,18 +1238,15 @@ 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 body = new FormData(); - body.append('foo', 'bar'); - const response = await page.request.fetch(`${baseURL}/actions/enhance?/${randomActionName}`, { method: 'POST', - body, + body: 'irrelevant', headers: { Origin: `${baseURL}` }