2013-04-03 63 views
0

我在我的Express應用程序的自定義錯誤處理程序。配置:快遞未知錯誤處理程序記錄的自定義錯誤

app.use(Routes.error); 
app.use(express.static(__dirname + '/assets')); 
app.use(express.cookieParser(config.app.sessionSecret)); 
app.use(express.bodyParser()); 
app.use(express.session({ 
    cookie: { 
     maxAge: 60000 
    } 
})); 
app.use(flashify); 
app.use(expressValidator); 
app.use(app.router); 

錯誤路由器:

error: function(err, req, res, next) { 
    res.render('error.jade', { 
     error: err.message 
    }, function(renderError, html) { 
     if (renderError) { 
      res.send(500, 'Error while displaying the error template!'); 
     } else { 
      res.send(err.code, html); 
     } 
    }); 
}, 

我的問題是,自定義錯誤消息在控制檯登錄:

//In the route 
next(new NotFoundError('Could not find the template!')); 

//Custom error 
var NotFoundError = function(message) { 
this.code = 404; 
this.message = message; 
}; 

util.inherits(NotFoundError, Error); 
NotFoundError.name = 'NotFoundError'; 

這是在非常不安單元測試:

✔ server - testTemplateCreate 
Error: Could not find the template! 
✔ server - testTemplateEdit 

這是正確的,因爲模板無法找到,但我不希望被顯示的消息。我想,如果您提供自定義錯誤路由,則不會使用本機錯誤功能。

回答

1

在其中安裝中間件的順序很重要。

您聲明您的錯誤處理程序在頂部,這意味着任何中間件和路由後面的聲明將而不是得到處理。相反,移動你的錯誤處理程序到路由器後:

... 
app.use(app.router); 
app.use(Routes.error); 
+0

謝謝你,它的工作!我已經讀過這個命令,並且改變了它的某些部分,但是並不認爲你必須把錯誤路由放在normaler路由器的後面。 – Tim

相關問題