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

问题描述

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

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');
      }
    });
});

为了让异常 2 执行,我注释掉异常 1.

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

推荐答案

问题是 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.

它与 exception2 的不同之处在于 '/' 路由的处理函数由 Connect 块直接执行,与原始调用在同一堆栈中通过快递.第二个异常发生在回调中,在一些 I/O 操作返回之后,因此由源自 Node 事件循环 I/O 处理程序的堆栈执行,因此 try/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 域的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:52