Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-snails-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Set correct `$page.status` when using `enhance` and result is of type `'error'`
1 change: 1 addition & 0 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export function enhance(form, submit = () => {}) {
});

result = deserialize(await response.text());
if (result.type === 'error') result.status = response.status;
} catch (error) {
if (/** @type {any} */ (error)?.name === 'AbortError') return;
result = { type: 'error', error };
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ export function create_client({ target, base }) {
url,
params: current.params,
branch: branch.slice(0, error_load.idx).concat(error_load.node),
status: 500, // TODO might not be 500?
status: result.status ?? 500,
error: result.error,
route
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { error } from '@sveltejs/kit';

/** @type {import('./$types').PageServerLoad} */
export function load() {
return {
Expand Down Expand Up @@ -27,5 +29,8 @@ export const actions = {
return {
result: 'submitter: ' + fields.get('submitter')
};
},
error: () => {
throw error(400, 'error');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<button class="form1">Submit</button>
<button class="form1-register" formAction="?/register">Submit</button>
<button class="form1-submitter" formAction="?/submitter" name="submitter" value="foo">Submit</button>
<button class="form1-error" formAction="?/error">Submit</button>
</form>

<span class="count">{count}</span>
Expand Down
11 changes: 11 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,17 @@ test.describe('Actions', () => {

expect(page.url()).toContain('/actions/enhance');
});

test('$page.status reflects error status', async ({ page, app }) => {
await page.goto('/actions/enhance');

await Promise.all([
page.waitForRequest((request) => request.url().includes('/actions/enhance')),
page.click('button.form1-error')
]);

await expect(page.locator('h1')).toHaveText('400');
});
});

// Run in serial to not pollute the log with (correct) cookie warnings
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ export type ActionResult<
| { type: 'success'; status: number; data?: Success }
| { type: 'failure'; status: number; data?: Invalid }
| { type: 'redirect'; status: number; location: string }
| { type: 'error'; error: any };
| { type: 'error'; status?: number; error: any };

/**
* Creates an `HttpError` object with an HTTP status code and an optional message.
Expand Down