Files
the-example-app-nodejs/handlers/errorHandlers.js
Khaled Garbaya f45c65c111 chore: Improve error page (#66)
* chore: Improve error page

* fix: Fix middlware order

* fix: Remove console.log
2017-11-15 14:01:54 +01:00

17 lines
454 B
JavaScript

/**
* Catch Errors Handler
* Instead of using try{} catch(e) {} in each controller, we wrap the function in
* catchErrors(), catch any errors they throw, and pass it along to our express middleware with next().
*/
module.exports.catchErrors = (fn) => {
return function (request, response, next) {
return fn(request, response, next).catch((e) => {
if (e.response) {
e.status = e.response.status
}
next(e)
})
}
}