我的快速应用程序中有一个自定义错误处理程序。配置:
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);
错误路由器:
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);
}
});
},
我遇到的问题是控制台中记录了一条自定义错误消息:
//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';
这在单元测试期间非常令人不安:
✔ server - testTemplateCreate
Error: Could not find the template!
✔ server - testTemplateEdit
找不到模板是正确的,但我不希望显示消息。我想,如果您提供自定义错误路由,则不会使用 native 错误函数。
最佳答案
安装中间件的顺序很重要。
您在顶部声明您的错误处理程序,这意味着在其声明之后的任何中间件和路由都不会被它处理。相反,将您的错误处理程序移动到路由器之后:
...
app.use(app.router);
app.use(Routes.error);
关于node.js - Express 未知错误处理程序正在记录自定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792913/