Skip to content

Commit 5b81a21

Browse files
authored
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"
1 parent d67425c commit 5b81a21

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

.changeset/warm-sloths-cry.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: correctly return 404 when a form action is not found

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ async function call_action(event, actions) {
216216

217217
const action = actions[name];
218218
if (!action) {
219-
throw new Error(`No action with name '${name}' found`);
219+
throw error(404, `No action with name '${name}' found`);
220220
}
221221

222222
if (!is_form_content_type(event.request)) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,24 @@ test.describe('Actions', () => {
12601260
expect(error.message).toBe('Actions expect form-encoded data (received application/json)');
12611261
expect(response.status()).toBe(415);
12621262
});
1263+
1264+
test('submitting to a form action that does not exists, should return http status code 404', async ({
1265+
baseURL,
1266+
page
1267+
}) => {
1268+
const randomActionName = 'some-random-action';
1269+
const response = await page.request.fetch(`${baseURL}/actions/enhance?/${randomActionName}`, {
1270+
method: 'POST',
1271+
body: 'irrelevant',
1272+
headers: {
1273+
Origin: `${baseURL}`
1274+
}
1275+
});
1276+
const { type, error } = await response.json();
1277+
expect(type).toBe('error');
1278+
expect(error.message).toBe(`No action with name '${randomActionName}' found`);
1279+
expect(response.status()).toBe(404);
1280+
});
12631281
});
12641282

12651283
// Run in serial to not pollute the log with (correct) cookie warnings

0 commit comments

Comments
 (0)