无法使用express处理node.js域的异常 [英] Unable to handle exception with node.js domains using express

查看:131
本文介绍了无法使用express处理node.js域的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Node.js域来捕获异常。它工作到目前为止,但有一个地方我无法获取域来捕获异常。回调中的exception2在domain.on('error')处理程序中被捕获和处理,但是exception1未被捕获。奇怪的是,当异常1被抛出时,它不像我所期望的那样关闭节点。这是我的示例应用程序:

I want to use Node.js Domains to catch exceptions. It is working so far, but there is one place I can't get domains to catch the exception. exception2 in the callback is caught and handled in the domain.on('error') handler, but exception1 is not caught. The odd thing is that when exception1 is thrown, it doesn't shutdown Node like I would expect. Here is my example app:

var domain = require('domain');
var request = require('request');
var express = require('express');

var serverDomain = domain.create();
serverDomain.on('error', function(err) {
  console.log("Server Domain Error: " + err);
});

var app;

serverDomain.run(function() {
  app = express();
  app.listen(3000);
});

app.use(function(req, res, next) {

  var reqDomain = domain.create();
  reqDomain.add(req);
  reqDomain.add(res);
  reqDomain.on('error', function(err) {
    console.log("Req Domain Error: " + err);
    reqDomain.dispose();
    next(err);
  });

  next();
});

app.get('/', function(req, res) {
  var uri = "http://google.com";

  exception1.go();

  request.get({url:uri, json: {}},
    function (error, response, body) {
      if(response.statusCode === 200) {
        exception2.go();
        res.send('Success getting google response');
      }
    });
});

为了让exception2执行,我注释掉异常1。

To get exception2 to execute, I comment out exception 1.

推荐答案

问题是在连接的路由,它同时具有 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.

它与 exception2 '/'路由的处理函数直接由该连接块执行,与通过Express的原始调用相同的堆栈。在一些I / O操作返回后,第二个异常发生在回调中,因此由源自Node的事件循环I / O处理程序的堆栈执行,因此 Express / catch 的Express不可用来阻止该异常并保存应用服务器。事实上,如果你注销掉所有的域名,并且旅行 exception2 它会崩溃Node。

How it differs from exception2 is that the handler function for the '/' route is executed directly by that Connect block, in the same stack as the original call that went through Express. The second exception occurs in a callback, after some I/O operation has returned, and therefore is executed by a stack originating from Node's event loop I/O handler, and so the try/catch of Express isn't available to snag that exception and save the app server. In fact, if you comment out all the domain stuff, and trip exception2 it crashes Node.

由于只有未处理异常被路由到域机制,并且由于 exception1 在其上面的调用栈中有一个 try / catch 可见,该异常被处理,而不是转发到该域。

Since only unhandled exceptions are routed to the domain mechanism, and since exception1 has a try/catch visible in it's call stack above it, the exception is handled, and not forwarded to the domain.

这篇关于无法使用express处理node.js域的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆