[Error] [ 'Error',
' at repl:1:7',
' at REPLServer.self.eval (repl.js:110:21)',
' at Interface.<anonymous> (repl.js:239:12)',
' at Interface.EventEmitter.emit (events.js:95:17)',
' at Interface._onLine (readline.js:202:10)',
' at Interface._line (readline.js:531:8)',
' at Interface._ttyWrite (readline.js:760:14)',
' at ReadStream.onkeypress (readline.js:99:10)',
' at ReadStream.EventEmitter.emit (events.js:98:17)',
' at emitKey (readline.js:1095:12)' ]
Error:
at /Users/cfisher/Git/squashed/execProcess.js:6:17
at ChildProcess.exithandler (child_process.js:213:5)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:498:12)
代替:
Error: Error: Command failed: sh ./commands/getBranchCommitCount.sh HEAD
git: 'rev-lists' is not a git command. See 'git --help'.
Did you mean this?
rev-list
at /Users/cfisher/Git/squashed/execProcess.js:6:17
at ChildProcess.exithandler (child_process.js:213:5)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:498:12)
9 回答
要以更易读的方式在控制台中打印
Error
的堆栈跟踪:示例结果:
现在有一个dedicated function on console:
试试Error.captureStackTrace(targetObject[, constructorOpt]) .
函数
a
和b
在错误堆栈中捕获并存储在myObj
中 .使用随时可用的节点模块,可以从节点获取全长堆栈跟踪(尽管性能损失较小):http://www.mattinsler.com/post/26396305882/announcing-longjohn-long-stack-traces-for-node-js
你可以使用node-stack-trace模块,它是一个电源满模块来跟踪调用堆栈 .
我知道打印nodejs中的完整堆栈跟踪是不可能的,您只需打印一个"partial"堆栈跟踪,您无法从代码中的哪个位置看到异常发生的位置 . 这就是Ryan Dahl在这个YouTube视频中解释的内容 . http://youtu.be/jo_B4LTHi3I在56:30准确无误 . 希望这可以帮助
如果您只想记录错误的堆栈跟踪(而不是错误消息),节点6及更高版本会自动在堆栈跟踪中包含错误名称和消息,如果您想要进行一些自定义错误处理,这有点烦人:
console.log(error.stack.replace(error.message, ''))
此解决方法将仅记录错误名称和堆栈跟踪(例如,您可以格式化错误消息并在代码中的其他位置显示它的方式) .
上面的示例仅打印堆栈跟踪后面的错误名称,例如:
代替:
如前所述,您只需使用trace命令即可:
但是, if you came to this question searching about how to log the stack trace of an exception ,您只需记录Exception对象即可 .
它会记录:
If your Node.js version is < than 6.0.0 ,记录Exception对象是不够的 . 在这种情况下,它只会打印:
对于节点版本<6,使用
console.error(e.stack)
而不是console.error(e)
来打印错误消息加上完整堆栈,就像当前节点版本一样 .Note: 如果将异常创建为类似
throw "myException"
的字符串,则无法检索堆栈跟踪并记录e.stack
产生 undefined .为了安全起见,您可以使用
它适用于新旧Node.js版本 .
任何
Error
对象都有一个stack
成员,用于捕获构造它的点 .或更简单地说: