节点,Express,域,未捕获的异常 - 仍然丢失 [英] Node, Express, domains, uncaught exceptions - still lost
问题描述
在Node中,我已经读取了几个小时的异常处理。我明白使用 uncaughtException
的缺点,据了解,关闭该过程有助于防止任何可能发生的事情的任何未知状态。我了解到,使用域是一种方式,我明白如何正确实施域,具体来说显式绑定 ...
I have been reading for hours on exception handling in Node. I understand the cons of using uncaughtException
, I understand that shutting down the process is good for preventing any "unknown state" where "anything can happen". I understand that using domains is the way to go, and I understand how to properly implement domains, specifically Explicit Binding...
...但是我仍然没有得到任何结果,只是基本的错误处理。
...but I'm still not getting any results for just basic error handling.
我想能够抓住任何未捕获的例外,目的是日志记录。我不介意杀死这个过程或者其他被认为是不合需要的东西。我只想要一个日志。
I would like to be able to just catch any uncaught exceptions for the purpose of logging. I don't mind killing the process or anything else deemed "undesirable". I just want a log.
我不觉得我应该把所有内容都放在一个try / catch中,或者使用一些库来 emit
错误...如果我错了,请纠正我,我会改变我的方式。
I don't feel like I should have to wrap everything in a try/catch or use some library to emit
errors... please correct me if I'm wrong and I will change my ways.
我正在使用Node和Express,以下简单的代码:
I am using Node and Express and I have the following simple code:
var express = require('express');
var domain = require('domain');
var serverDomain = domain.create();
serverDomain.on('error', function(err) {
console.log("SERVER DOMAIN ERROR: " + err.message);
});
serverDomain.run(function() {
var app = express();
app.get('/testing', function() {
app.nonExistent.call(); // this throws an error
});
var server = app.listen(8000, function() {
console.log('Listening on port %d', server.address().port);
});
});
错误显示在控制台中,但控制台从未收到SERVER DOMAIN ERROR ... 信息。我也试图在自己的域中包装请求/响应,无济于事。更令人失望的是,使用以下内容也不起作用:
The error shows up in the console, but the console never receives the "SERVER DOMAIN ERROR..." message. I have also tried wrapping the request/response in their own domain as well, to no avail. Even more disappointing is the fact that using the following does not work either:
process.on('uncaughtException', function(err) {
console.log('uncaughtException caught the error');
});
我做错了吗?我从哪里去?如何捕捉上述错误?
Am I doing something wrong? Where do I go from here? How can I catch the above error?
推荐答案
您可以使用 connect-domain 。
问题是在Connect的路由中发生异常,一个围绕其执行的try / catch块,以及一个在非生产模式下运行时打印出堆栈跟踪详细信息的默认错误处理程序。由于异常在Express中处理,所以它永远不会到达您的外层以供域处理。
The problem is that the exception happens during Connect's routing, which has both a try/catch block around its execution, as well as a default error handler which prints out stack trace details when running in a non-production mode. Since the exception is handled inside of Express, it never reaches your outer layer for the domains to handle.
这是一个为什么要使用connect-domain包而不是域的示例。
Here is an example why to use connect-domain package instead of domain.
http://masashi-k.blogspot.com/2012/12/express3-global-error-handling-domain.html
var express = require('express');
var connectDomain = require('connect-domain');
var app = express();
app.use(connectDomain());
app.get('/testing', function() {
app.nonExistent.call(); // this throws an error
});
app.use(function(err, req, res, next) {
res.end(err.message); // this catches the error!!
});
var server = app.listen(8000, function() {
console.log('Listening on port %d', server.address().port);
});
这篇关于节点,Express,域,未捕获的异常 - 仍然丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!