如果是使用nodejs+express3这个经典的组合,那么有一种很方面的处理回调函数异常的方法:
1. 安装模块:express-domain-middleware
2. 加入如下的代码:
app.use(require('express-domain-middleware'));
app.use(function errorHandler(err, req, res, next) {
logger.error('error on request %d %s %s: %j', process.domain.id, req.method, req.url, err);
res.send(500, "there is an error in callback function");
if(err.domain) {
//you should think about gracefully stopping & respawning your server
//since an unhandled error might put your application into an unknown state
}
});
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)