feat(handlers): Add generic error handler

This commit is contained in:
Khaled Garbaya
2017-10-04 10:36:04 +02:00
committed by Benedikt Rötsch
parent 01fd3cc9b3
commit 67fc0edf62
6 changed files with 42 additions and 30 deletions

12
handlers/errorHandlers.js Normal file
View File

@@ -0,0 +1,12 @@
/*
Catch Errors Handler
With async/await, you need some way to catch errors
Instead of using try{} catch(e) {} in each controller, we wrap the function in
catchErrors(), catch and errors they throw, and pass it along to our express middleware with next()
*/
exports.catchErrors = (fn) => {
return function (req, res, next) {
return fn(req, res, next).catch(next)
}
}