diff --git a/templates/src/server/middleware/get_server_route_handler.ts b/templates/src/server/middleware/get_server_route_handler.ts index c788f4dcb..82707777f 100644 --- a/templates/src/server/middleware/get_server_route_handler.ts +++ b/templates/src/server/middleware/get_server_route_handler.ts @@ -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); } diff --git a/test/apps/errors/src/routes/async-throw.json.js b/test/apps/errors/src/routes/async-throw.json.js new file mode 100644 index 000000000..918049e61 --- /dev/null +++ b/test/apps/errors/src/routes/async-throw.json.js @@ -0,0 +1,3 @@ +export async function get(req, res) { + throw new Error('oops'); +} \ No newline at end of file diff --git a/test/apps/errors/test.ts b/test/apps/errors/test.ts index 158bbbcb5..60b639751 100644 --- a/test/apps/errors/test.ts +++ b/test/apps/errors/test.ts @@ -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();