Skip to content
Closed
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
17 changes: 16 additions & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,22 @@ export async function render_endpoint(event, mod) {
});
}

const response = await handler(event);
let response;
try {
response = await handler(event);
} catch (err) {
const parsed_status = parseInt(err.status);
if (isNaN(parsed_status)) {
throw err;
}
response = {
status: parsed_status,
headers: err.headers,
body: err.body,
error: err.error
};
}

const preface = `Invalid response from route ${event.url.pathname}`;

if (typeof response !== 'object') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
const err = new Error();
err.status = 200;
err.body = { hello: 'world' };
throw err;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
const err = new Error();
err.status = 555;
throw err;
}
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-thrown-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>
24 changes: 24 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,12 @@ test.describe.parallel('Endpoints', () => {
expect(await response.json()).toEqual({});
});

test('200 status on thrown response', async ({ request }) => {
const response = await request.get('/endpoint-output/thrown-ok');
expect(/** @type {import('@playwright/test').APIResponse} */ (response).status()).toBe(200);
expect(await response.json()).toEqual({ hello: 'world' });
});

test('set-cookie without body', async ({ request }) => {
const response = await request.get('/endpoint-output/headers');
expect(/** @type {import('@playwright/test').APIResponse} */ (response).status()).toBe(200);
Expand Down Expand Up @@ -1002,6 +1008,24 @@ test.describe.parallel('Errors', () => {
}
});

test('thrown not ok response from endpoint', async ({ page, read_errors }) => {
const res = await page.goto('/errors/endpoint-thrown-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-thrown-not-ok\.svelte:12:9|endpoint-thrown-not-ok\.svelte:12:15/; // TODO: Remove second location with Vite 2.9

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');

Expand Down