【问题标题】:How do I shut down my process after using Raygun's express-handler?使用 Raygun 的 express-handler 后如何关闭我的进程?
【发布时间】:2015-07-12 18:44:38
【问题描述】:

我正在使用 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 就不起作用了。

做什么?

【问题讨论】:

    标签: node.js express error-handling raygun raygun.io


    【解决方案1】:

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

    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 作为第五个参数,这在默认错误处理程序中是不可能的。

    【讨论】:

    • 啊!应该只看源代码。没想到 expressHandler 这么简单。
    猜你喜欢
    • 2019-04-04
    • 2019-12-27
    • 1970-01-01
    • 2018-12-21
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多