Skip to content

Commit 96751b0

Browse files
authored
[fix] set correct $page.status when using enhance and result is of type 'error' (#8073)
Fixes #8072
1 parent a2b9c3e commit 96751b0

File tree

7 files changed

+25
-2
lines changed

7 files changed

+25
-2
lines changed

.changeset/dry-snails-own.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+
Set correct `$page.status` when using `enhance` and result is of type `'error'`

packages/kit/src/runtime/app/forms.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export function enhance(form, submit = () => {}) {
107107
});
108108

109109
result = deserialize(await response.text());
110+
if (result.type === 'error') result.status = response.status;
110111
} catch (error) {
111112
if (/** @type {any} */ (error)?.name === 'AbortError') return;
112113
result = { type: 'error', error };

packages/kit/src/runtime/client/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ export function create_client({ target, base }) {
13871387
url,
13881388
params: current.params,
13891389
branch: branch.slice(0, error_load.idx).concat(error_load.node),
1390-
status: 500, // TODO might not be 500?
1390+
status: result.status ?? 500,
13911391
error: result.error,
13921392
route
13931393
});

packages/kit/test/apps/basics/src/routes/actions/enhance/+page.server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { error } from '@sveltejs/kit';
2+
13
/** @type {import('./$types').PageServerLoad} */
24
export function load() {
35
return {
@@ -27,5 +29,8 @@ export const actions = {
2729
return {
2830
result: 'submitter: ' + fields.get('submitter')
2931
};
32+
},
33+
error: () => {
34+
throw error(400, 'error');
3035
}
3136
};

packages/kit/test/apps/basics/src/routes/actions/enhance/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<button class="form1">Submit</button>
2121
<button class="form1-register" formAction="?/register">Submit</button>
2222
<button class="form1-submitter" formAction="?/submitter" name="submitter" value="foo">Submit</button>
23+
<button class="form1-error" formAction="?/error">Submit</button>
2324
</form>
2425

2526
<span class="count">{count}</span>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,17 @@ test.describe('Actions', () => {
20192019

20202020
expect(page.url()).toContain('/actions/enhance');
20212021
});
2022+
2023+
test('$page.status reflects error status', async ({ page, app }) => {
2024+
await page.goto('/actions/enhance');
2025+
2026+
await Promise.all([
2027+
page.waitForRequest((request) => request.url().includes('/actions/enhance')),
2028+
page.click('button.form1-error')
2029+
]);
2030+
2031+
await expect(page.locator('h1')).toHaveText('400');
2032+
});
20222033
});
20232034

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

packages/kit/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ export type ActionResult<
10741074
| { type: 'success'; status: number; data?: Success }
10751075
| { type: 'failure'; status: number; data?: Invalid }
10761076
| { type: 'redirect'; status: number; location: string }
1077-
| { type: 'error'; error: any };
1077+
| { type: 'error'; status?: number; error: any };
10781078

10791079
/**
10801080
* Creates an `HttpError` object with an HTTP status code and an optional message.

0 commit comments

Comments
 (0)