Better async error handling
This commit is contained in:
14
modules/utils/asyncHandler.js
Normal file
14
modules/utils/asyncHandler.js
Normal 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);
|
||||
});
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user