Appearance
Global Error Handler
As error handling is one of the important aspect for any application. So, there is a global error handler which catches the error and exceptions. In other to trigger this global error handler we need to wrap the associated function binded to the specific route by (wrapNext()) function as it resolves the pending promises and catches any exceptions.
module.exports = (fn) => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};Similarly, (src/handler/error-handler.js) file is reponsible for handling all the exceptions and to take necessary actions accordingly.
const errorHandler = (err, req, res, next) => {
if (!Boom.isBoom(err)) {
err = Boom.boomify(err);
}
const { statusCode, message } = err.output.payload;
if (process.env.APP_DEBUG === "true") {
return res.render("error/error-stacktrace", {
statusCode: statusCode,
errorMessage: message,
stackTrace: err.stack,
reqMethod: req.method,
reqUrl: `${process.env.CMSURL}${req.originalUrl}`
});
}
return res.render(`error/${statusCode}`, err.output.payload);
};
module.exports = errorHandler;In the above code snippet an env variable (APP_DEBUG) is used to show the traced error view page or just the simple error page.