有人知道如何在Node.js中打印堆栈跟踪吗?
当前回答
如果你只想记录错误的堆栈跟踪(而不是错误消息),Node 6及以上会自动在堆栈跟踪中包含错误名称和消息,如果你想做一些自定义错误处理,这有点烦人:
console.log (error.stack.replace(错误。信息,”)
这个解决方案将只记录错误名称和堆栈跟踪(因此,例如,您可以格式化错误消息,并在代码中的其他地方以您希望的方式显示它)。
上面的例子只打印错误名称,后面跟着堆栈跟踪,例如:
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)
其他回答
任何Error对象都有一个堆栈成员,该成员捕获了构造它的点。
var stack = new Error().stack
console.log( stack )
或者更简单地说:
console.trace("Here I am!")
现在主机上有一个专门的功能:
console.trace()
您可以使用node-stack-trace模块,这是一个强大的完整模块来跟踪调用堆栈。
如果你只想记录错误的堆栈跟踪(而不是错误消息),Node 6及以上会自动在堆栈跟踪中包含错误名称和消息,如果你想做一些自定义错误处理,这有点烦人:
console.log (error.stack.replace(错误。信息,”)
这个解决方案将只记录错误名称和堆栈跟踪(因此,例如,您可以格式化错误消息,并在代码中的其他地方以您希望的方式显示它)。
上面的例子只打印错误名称,后面跟着堆栈跟踪,例如:
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)
尝试错误。captureStackTrace (constructorOpt targetObject[])。
const myObj = {};
function c() {
// pass
}
function b() {
Error.captureStackTrace(myObj)
c()
}
function a() {
b()
}
a()
console.log(myObj.stack)
函数a和b在错误堆栈中被捕获并存储在myObj中。
推荐文章
- JavaScript: override alert()
- 重置setTimeout
- 如何确保<select>表单字段被禁用时提交?
- jQuery有不聚焦的方法吗?
- 反应钩子-正确的方式清除超时和间隔
- TypeScript枚举对象数组
- 在React.js中正确的img路径
- 在React.js中更新组件onScroll的样式
- onClick ReactJS调用多个函数
- 如何在JavaScript中转义单引号(')?
- Ng-repeat结束事件
- 谷歌MAP API未捕获的类型错误:无法读取属性“offsetWidth”为空
- 模糊vs聚焦-有什么真正的区别吗?
- 如何使用JavaScript创建和样式一个div ?
- 清除JavaScript中的缓存