I have a custom error handler in my express application. The configuration:
app.use(Routes.error);
app.use(express.static(__dirname + '/assets'));
app.use(express.cookieParser(config.app.sessionSecret));
app.use(express.bodyParser());
app.use(express.session({
cookie: {
maxAge: 60000
}
}));
app.use(flashify);
app.use(expressValidator);
app.use(app.router);
The error router:
error: function(err, req, res, next) {
res.render('error.jade', {
error: err.message
}, function(renderError, html) {
if (renderError) {
res.send(500, 'Error while displaying the error template!');
} else {
res.send(err.code, html);
}
});
},
The problem I have is that a custom error message is logged in the console:
//In the route
next(new NotFoundError('Could not find the template!'));
//Custom error
var NotFoundError = function(message) {
this.code = 404;
this.message = message;
};
util.inherits(NotFoundError, Error);
NotFoundError.name = 'NotFoundError';
This is very disturbing during the unit tests:
✔ server - testTemplateCreate
Error: Could not find the template!
✔ server - testTemplateEdit
It is correct that the template could not be found, but I do not want the message to be displayed. I thought, if you provide a custom error route, no native error function is used.
The order in which you install middleware matters.
You declare your error handler right at the top, which means that any middleware and routes following its declaration will not get handled by it. Instead, move your error handler to after the router:
...
app.use(app.router);
app.use(Routes.error);