- Place errorHandler after all routes and before
.listen
- Forget
process.env.NODE_ENV
- Use knas when raising errors if you need to produce http status codes
function errorHandler(err, req, res, next) {
// err is either obj or string
res.status(err.status || 500).send(err.message || err);
};
// routes here..
app.use(errorHandler);
// .listen
$.ajax({
error: function(a, b, c) {
// a.responseText is the actual error string (!) passed from the server like err.message
// a.statusText is the http status code
// b is a string about timeouts and parser errors
// c is the same as a.statusText
next(a.responseText || a.statusText);
}
});
Ajax handlers vs http status codes
2XX
will run successHandler3xx - 5xx
will run errorHandler
Relevant http status codes
400
bad request (from client)401
unauthorized404
not found500
internal server error