如果是使用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 }));



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章:

  • 2022-12-23
  • 2021-07-15
  • 2021-09-08
  • 2021-12-13
  • 2022-12-23
  • 2021-08-02
  • 2021-10-21
  • 2022-02-10
猜你喜欢
  • 2022-03-10
  • 2021-07-30
  • 2021-06-01
  • 2022-12-23
  • 2021-04-10
  • 2021-09-06
相关资源
相似解决方案