Skip to content

Commit

Permalink
fix: action not found should return 404 (#11278)
Browse files Browse the repository at this point in the history
When posting to a form action, that does exist, the server returned
500. With this fix, it will return 404 error "not found"
  • Loading branch information
alexbjorlig authored Dec 13, 2023
1 parent d67425c commit 5b81a21
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/warm-sloths-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly return 404 when a form action is not found
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
18 changes: 18 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b81a21

Please sign in to comment.