1

我有一个标准的套接字服务器(NO HTTP)设置如下(人为):

var server = net.createServer(function(c) { //'connection' listener
  c.on('data', function(data) {
    //do stuff here
    //some stuff can result in an exception that isn't caught anywhere downstream, 
    //so it bubbles up. I try to catch it here. 
    //this is the same problem as just trying to catch this: 
    throw new Error("catch me if you can");
  });
}).listen(8124, function() { //'listening' listener
   console.log('socket server started on port 8124,');
});

现在问题是我有一些代码抛出错误,根本没有被捕获,导致服务器崩溃。作为最后一项措施,我想在这个级别上抓住他们,但我尝试过的任何事情都失败了。

  • server.on("error",....)
  • c.on("error",...)

也许我需要到达套接字而不是c(连接),尽管我不确定如何。

我在节点 0.6.9

谢谢。

4

2 回答 2

3
process.on('uncaughtException',function(err){
   console.log('something terrible happened..')
})
于 2012-09-26T23:16:46.963 回答
0

您应该自己捕获异常。连接或服务器对象上没有任何事件可以让您按照您描述的方式处理异常。您应该在事件处理程序中添加异常处理逻辑,以避免像这样的服务器崩溃:

c.on('data', function(data) {
  try {
     // even handling code
  }
  catch(exception) {
    // exception handling code
  }
于 2012-09-26T19:49:07.567 回答