Skip to content

throw load validation errors so they are caught by handleError #4953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 23, 2022
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/famous-birds-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Throw load validation errors so that they are caught by handleError
19 changes: 5 additions & 14 deletions packages/kit/src/runtime/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,13 @@ export function normalize(loaded) {

if (loaded.redirect) {
if (!loaded.status || Math.floor(loaded.status / 100) !== 3) {
return {
status: 500,
error: new Error(
'"redirect" property returned from load() must be accompanied by a 3xx status code'
)
};
throw new Error(
'"redirect" property returned from load() must be accompanied by a 3xx status code'
);
}

if (typeof loaded.redirect !== 'string') {
return {
status: 500,
error: new Error('"redirect" property returned from load() must be a string')
};
throw new Error('"redirect" property returned from load() must be a string');
}
}

Expand All @@ -67,10 +61,7 @@ export function normalize(loaded) {
!Array.isArray(loaded.dependencies) ||
loaded.dependencies.some((dep) => typeof dep !== 'string')
) {
return {
status: 500,
error: new Error('"dependencies" property returned from load() must be of type string[]')
};
throw new Error('"dependencies" property returned from load() must be of type string[]');
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/kit/test/apps/basics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"preview": "node ../../cli.js preview",
"check": "tsc && svelte-check",
"test": "npm run test:dev && npm run test:build",
"test:dev": "cross-env DEV=true playwright test",
"test:build": "playwright test"
"test:dev": "rimraf test/errors.json && cross-env DEV=true playwright test",
"test:build": "rimraf test/errors.json && playwright test"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
Expand Down
16 changes: 15 additions & 1 deletion packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,13 @@ test.describe.parallel('Redirects', () => {
}
});

test('errors on missing status', async ({ baseURL, page, clicknav }) => {
test('errors on missing status', async ({
baseURL,
page,
clicknav,
javaScriptEnabled,
read_errors
}) => {
await page.goto('/redirect');

await clicknav('[href="/redirect/missing-status/a"]');
Expand All @@ -1928,6 +1934,14 @@ test.describe.parallel('Redirects', () => {
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: ""redirect" property returned from load() must be accompanied by a 3xx status code"'
);

if (!javaScriptEnabled) {
// handleError is not invoked for client-side navigation
const lines = read_errors('/redirect/missing-status/a').split('\n');
expect(lines[0]).toBe(
'Error: "redirect" property returned from load() must be accompanied by a 3xx status code'
);
}
});

test('errors on invalid status', async ({ baseURL, page, clicknav }) => {
Expand Down