Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

handle async route errors #488

Merged
merged 2 commits into from
Oct 25, 2018
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
7 changes: 6 additions & 1 deletion templates/src/server/middleware/get_server_route_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions test/apps/errors/src/routes/async-throw.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function get(req, res) {
return Promise.reject(new Error('oops'));
}
9 changes: 9 additions & 0 deletions test/apps/errors/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down