Skip to content

Commit

Permalink
handle async route errors
Browse files Browse the repository at this point in the history
Related to sveltejs#487
  • Loading branch information
nikku committed Oct 20, 2018
1 parent e5d7d8a commit 464924e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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 async function get(req, res) {
throw 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

0 comments on commit 464924e

Please sign in to comment.