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/selfish-walls-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Ensure handleError hook is called for shadow endpoint errors
9 changes: 7 additions & 2 deletions packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function load_node({
? await load_shadow_data(
/** @type {import('types/internal').SSRPage} */ (route),
event,
options,
!!state.prerender
)
: {};
Expand Down Expand Up @@ -360,10 +361,11 @@ export async function load_node({
*
* @param {import('types/internal').SSRPage} route
* @param {import('types/hooks').RequestEvent} event
* @param {import('types/internal').SSROptions} options
* @param {boolean} prerender
* @returns {Promise<import('types/endpoint').ShadowData>}
*/
async function load_shadow_data(route, event, prerender) {
async function load_shadow_data(route, event, options, prerender) {
if (!route.shadow) return {};

try {
Expand Down Expand Up @@ -440,9 +442,12 @@ async function load_shadow_data(route, event, prerender) {

return data;
} catch (e) {
const error = coalesce_to_error(e);
options.handle_error(error, event);

return {
status: 500,
error: coalesce_to_error(e)
error
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return {
status: 555
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch }) {
const res = await fetch('/errors/endpoint-not-ok.json');
if (res.ok) {
return {
props: await res.json()
};
} else {
return {
status: res.status,
error: new Error(res.statusText)
};
}
}
</script>

<h1>this text should not appear</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return {
status: 555
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<h1>this text should not appear</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
throw new Error('nope');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<h1>this text should not appear</h1>
55 changes: 55 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,61 @@ test.describe.parallel('Errors', () => {
}
});

test('not ok response from endpoint', async ({ page, read_errors }) => {
const res = await page.goto('/errors/endpoint-not-ok');

expect(read_errors('/errors/endpoint-not-ok.json')).toBeUndefined();

expect(res && res.status()).toBe(555);
expect(await page.textContent('#message')).toBe('This is your custom error page saying: ""');

const contents = await page.textContent('#stack');
const location = 'endpoint-not-ok.svelte:12:15';

if (process.env.DEV) {
expect(contents).toMatch(location);
} else {
expect(contents).not.toMatch(location);
}
});

test('error in shadow endpoint', async ({ page, read_errors }) => {
const res = await page.goto('/errors/endpoint-shadow');

// should include stack trace
const lines = read_errors('/errors/endpoint-shadow').split('\n');
expect(lines[0]).toMatch('nope');

if (process.env.DEV) {
expect(lines[1]).toMatch('endpoint-shadow');
}

expect(res && res.status()).toBe(500);
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "nope"'
);

const contents = await page.textContent('#stack');
const location = 'endpoint-shadow.js:1:8'; // TODO this is the wrong location, but i'm not going to open the sourcemap can of worms just now

if (process.env.DEV) {
expect(contents).toMatch(location);
} else {
expect(contents).not.toMatch(location);
}
});

test('not ok response from shadow endpoint', async ({ page, read_errors }) => {
const res = await page.goto('/errors/endpoint-shadow-not-ok');

expect(read_errors('/errors/endpoint-shadow-not-ok')).toBeUndefined();

expect(res && res.status()).toBe(555);
expect(await page.textContent('#message')).toBe(
'This is your custom error page saying: "Failed to load data"'
);
});

test('server-side 4xx status without error from load()', async ({ page }) => {
const response = await page.goto('/errors/load-status-without-error-server');

Expand Down