Add production error handler
This commit is contained in:
44
app.js
44
app.js
@ -15,6 +15,7 @@ process.on('uncaughtException', function (err) {
|
||||
});
|
||||
|
||||
var express = require('express'),
|
||||
accepts = require('accepts'),
|
||||
cookieParser = require('cookie-parser'),
|
||||
compress = require('compression'),
|
||||
session = require('express-session'),
|
||||
@ -524,17 +525,54 @@ app.get(
|
||||
}
|
||||
);
|
||||
|
||||
//put this route last
|
||||
app.get('/induce-vomiting', function(req, res, next) {
|
||||
next(new Error('vomiting induced'));
|
||||
});
|
||||
|
||||
// put this route last
|
||||
app.get(
|
||||
'/:username',
|
||||
userController.returnUser
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* 500 Error Handler.
|
||||
*/
|
||||
app.use(errorHandler());
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
app.use(errorHandler({ log: true }));
|
||||
} else {
|
||||
// error handling in production
|
||||
app.use(function(err, req, res, next) {
|
||||
|
||||
// respect err.status
|
||||
if (err.status) {
|
||||
res.statusCode = err.status;
|
||||
}
|
||||
|
||||
// default status code to 500
|
||||
if (res.statusCode < 400) {
|
||||
res.statusCode = 500;
|
||||
}
|
||||
|
||||
// parse res type
|
||||
var accept = accepts(req);
|
||||
var type = accept.type('html', 'json', 'text');
|
||||
|
||||
var message = 'opps! Something went wrong. Please try again later';
|
||||
if (type === 'html') {
|
||||
req.flash('errors', { msg: message });
|
||||
return res.redirect('/');
|
||||
// json
|
||||
} else if (type === 'json') {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
return res.send({ message: message });
|
||||
// plain text
|
||||
} else {
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
return res.send(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Start Express server.
|
||||
|
@ -15,6 +15,7 @@
|
||||
"postinstall": "node seed_data/seed.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"accepts": "^1.2.5",
|
||||
"async": "^0.9.0",
|
||||
"bcrypt-nodejs": "^0.0.3",
|
||||
"body-parser": "^1.9.3",
|
||||
|
Reference in New Issue
Block a user