如何在 NodeJS 中调试套接字挂断错误? [英] How to debug a socket hang up error in NodeJS?

查看:30
本文介绍了如何在 NodeJS 中调试套接字挂断错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

events.js:48
        throw arguments[1]; // Unhandled 'error' event
        ^
Error: socket hang up
    at createHangUpError (http.js:1091:15)
    at Socket.onend (http.js:1154:27)
    at TCP.onread (net.js:363:26)

在节点 v0.6.6 中,我的代码有多个 http.request 和 .get 调用.请建议跟踪导致套接字挂断的原因的方法,以及它是在哪个请求/调用上.谢谢

In node v0.6.6, my code has multiple http.request and .get calls. Please suggest ways to track what causes the socket hang up, and on which request/call it is. Thank you

推荐答案

快速而肮脏的开发解决方案:

使用 longjohn,您将获得包含异步操作的长堆栈跟踪.

Use longjohn, you get long stack traces that will contain the async operations.

干净且正确的解决方案:从技术上讲,在节点中,每当 您发出 'error' 事件没有人听它,它会抛出.为了让它不抛出,在它上面放一个监听器并自己处理.这样你就可以用更多的信息记录错误.

Clean and correct solution: Technically, in node, whenever you emit an 'error' event and no one listens to it, it will throw. To make it not throw, put a listener on it and handle it yourself. That way you can log the error with more information.

要为一组调用设置一个侦听器,您可以使用 并在运行时捕获其他错误.确保每个与 http(Server/Client) 相关的异步操作都在不同的 domain 上下文中与代码的其他部分相比,域将自动侦听 error 事件并将其传播到它自己的处理程序.所以你只听那个处理程序并获取错误数据.您还可以免费获得更多信息.(域已弃用).

To have one listener for a group of calls you can use domains and also catch other errors on runtime. Make sure each async operation related to http(Server/Client) is in different domain context comparing to the other parts of the code, the domain will automatically listen to the error events and will propagate it to it's own handler. So you only listen to that handler and get the error data. You also get more information for free.(Domains are depreceated).

正如 Mike 所建议的,您也可以设置 NODE_DEBUG=net 或使用 strace.它们都为您提供了节点在内部做什么.

As Mike suggested you can also set NODE_DEBUG=net or use strace. They both provide you what is node doing internally.

这篇关于如何在 NodeJS 中调试套接字挂断错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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