2015-07-12 32 views
1

我正在用NodeJS和Express構建一個應用程序,並最近在Raygun中加入了錯誤處理。如何在使用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的快速處理程序不起作用。

什麼?

回答

2

您可以定義自定義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爲第五參數,它是不可能的defaul錯誤處理的能力。

+1

啊!應該只查看源代碼。沒有意識到expressHandler是如此基本。 – tcmoore

相關問題