Better async error handling

This commit is contained in:
Michael Jackson
2019-07-30 17:06:27 -07:00
parent 7582c641fb
commit 7f90203a66
9 changed files with 102 additions and 46 deletions

View File

@ -0,0 +1,14 @@
/**
* Useful for wrapping `async` request handlers so they
* automatically propagate errors.
*/
export default function asyncHandler(handler) {
return (req, res, next) => {
Promise.resolve(handler(req, res, next)).catch(error => {
console.error(`Unexpected error in ${handler.name}!`);
console.error(error);
next(error);
});
};
}