如何让JavaScript异常更具信息性?

Mus*_*ore 5 javascript node.js

关于JavaScript的一个非常令人沮丧的事情是,如果它在外部模块上失败,它就不会告诉你启动路径的代码行.

例如:

Unhandled rejection RequestError: Error: socket hang up
    at new RequestError (/../node_modules/request-promise/lib/errors.js:11:15)
    at Request.RP$callback [as _callback] (/../node_modules/request-promise/lib/rp.js:60:32)
    at self.callback (/../node_modules/request/request.js:199:22)
    at Request.emit (events.js:95:17)
    at Request.onRequestError (/../node_modules/request/request.js:821:8)
    at ClientRequest.emit (events.js:95:17)
    at Socket.socketOnEnd (http.js:1569:9)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:117:20)
    at _stream_readable.js:944:16
    at process._tickDomainCallback (node.js:492:13)
Run Code Online (Sandbox Code Playgroud)

在什么现实中这个堆栈跟踪有用?有没有办法让它打印导致异常的最后一个"非node_module"调用?

for*_*ert 0

有几个节点模块提供“异步堆栈跟踪”。它们似乎提供了与AsyncChrome 开发人员工具中的复选框类似的功能(在“来源”下)。我从未真正使用过它们中的任何一个,并且我认为它们不一定适用于生产系统,但它们很可能对开发环境有用。

快速的谷歌搜索会向您显示许多可能的模块(例如async-stacktrace)。