使用Raygun的快速处理程序后如何关闭进程?



我正在用NodeJS和Express构建一个应用程序,最近与Raygun合并了错误处理。

Raygun提供了一个专门用于Express的错误处理程序,但它必须是运行的中间件的最后一部分。现在我有:

//Log the domain id, req, and error to the console
app.use(function errorHandler(err, req, res, next) {
  console.log('error on request %d %s %s: %s', process.domain.id, req.method, req.url, err)
  next(err)
})
//Log the error to Raygun
//!Must be last piece of middleware to be defined
app.use(raygunClient.expressHandler);

但问题是服务器继续运行,可能会使应用程序处于未知状态。我想做一个优雅的服务器关闭,但是如果我添加

//Shuts down the process
app.use(function errorHandler(err, req, res, next) {
  process.exit()
})

则Raygun的express-handler不工作。

什么?

您可以定义自定义的raygunClient.expressHandler,就像在库中定义的那样。

app.use(function errorHandler(err, req, res, next) {
  console.log('error on request %d %s %s: %s', process.domain.id, req.method, req.url, err)
  next(err)
}) 
app.use(function (err, req, res, next) {
  raygunClient.send(err, {}, function (response) {
    //all done
    process.exit(1);
  }, req);
  next(err);
});

现在你也有能力发送customData作为第二个参数和tags作为第五个参数,这是不可能的默认错误处理程序

最新更新