50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
var appEnv = process.env.APP_ENV;
|
|
if (appEnv == undefined) {
|
|
appEnv = 'development';
|
|
}
|
|
const isProduction = appEnv === 'production',
|
|
chalk = require('chalk'),
|
|
utils = require('../routes/utils');
|
|
|
|
const exceptionHandler = {
|
|
notFoundErrorHandler: (req, res, next) => {
|
|
let err = new Error('Not Found');
|
|
err.status = 404;
|
|
console.log(`${utils.getDateTimeISOString()}`, chalk.yellow(`${err.status} ${err.message}`));
|
|
next(err);
|
|
},
|
|
logErrorHandler: (err, req, res, next) => {
|
|
console.log(`${utils.getDateTimeISOString()}`, chalk.red(`error handler : ${err.message} ${err.err}`));
|
|
console.error(err.stack);
|
|
next(err);
|
|
},
|
|
clientErrorHandler: (err, req, res, next) => {
|
|
if (req.xhr) {
|
|
res.status(err.status || 500);
|
|
} else {
|
|
next(err);
|
|
}
|
|
},
|
|
// eslint-disable-next-line no-unused-vars
|
|
fianlErrorHandler: (err, req, res, next) => {
|
|
res.status(err.status || 500);
|
|
if (!isProduction) {
|
|
/* development error handler will print stacktrace */
|
|
res.json({errors: {
|
|
message: err.message,
|
|
error: err.err,
|
|
stack: err.stack
|
|
}});
|
|
} else {
|
|
/* production error handler no stacktraces leaked to user */
|
|
res.json({errors: {
|
|
message: err.message,
|
|
error: {},
|
|
stack: {}
|
|
}});
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = exceptionHandler;
|