handle async route errors

Related to #487
This commit is contained in:
Nico Rehwaldt
2018-10-20 22:40:21 +02:00
parent e5d7d8ab2b
commit 464924ed67
3 changed files with 18 additions and 1 deletions

View File

@@ -53,7 +53,12 @@ export function get_server_route_handler(routes: ServerRoute[]) {
};
try {
handle_method(req, res, handle_next);
const result = handle_method(req, res, handle_next);
// catch failures in async functions
if (Promise.resolve(result) === result) {
result.catch(handle_next);
}
} catch (err) {
handle_next(err);
}

View File

@@ -0,0 +1,3 @@
export async function get(req, res) {
throw new Error('oops');
}

View File

@@ -112,6 +112,15 @@ describe('errors', function() {
);
});
it('does not serve error page for async non-page error', async () => {
await page.goto(`${base}/async-throw.json`);
assert.equal(
await page.evaluate(() => document.body.textContent),
'oops'
);
});
it('clears props.error on successful render', async () => {
await page.goto(`${base}/no-error`);
await start();