unpkg/modules/utils/asyncHandler.js

15 lines
381 B
JavaScript
Raw Normal View History

2019-07-31 00:06:27 +00:00
/**
2019-08-05 06:49:34 +00:00
* Useful for wrapping `async` request handlers in Express
* so they automatically propagate errors.
2019-07-31 00:06:27 +00:00
*/
export default function asyncHandler(handler) {
return (req, res, next) => {
Promise.resolve(handler(req, res, next)).catch(error => {
2021-10-05 04:54:12 +00:00
console.error(`Unexpected error in ${handler.name}!`);
console.error(error.stack);
2019-07-31 00:06:27 +00:00
next(error);
});
};
}