简体   繁体   English

如何在没有 console.log 的情况下访问与 console.log(Error('example')) 相同的功能

[英]how to access same functionality as console.log(Error('example')) without console.log

example:例子:

> console.log(Error('test'))
Error: test
    at repl:1:13
    at Script.runInThisContext (vm.js:116:20)
    at REPLServer.defaultEval (repl.js:404:29)
    at bound (domain.js:420:14)
    at REPLServer.runBound [as eval] (domain.js:433:12)
    at REPLServer.onLine (repl.js:715:10)
    at REPLServer.emit (events.js:215:7)
    at REPLServer.EventEmitter.emit (domain.js:476:20)
    at REPLServer.Interface._onLine (readline.js:316:10)
    at REPLServer.Interface._line (readline.js:693:8)

I want to store this entire string with stack as a string.我想将整个字符串与堆栈一起存储为字符串。 But this doesn't work, it doesn't have the stack.但这不起作用,它没有堆栈。 How can I access the same functionality?如何访问相同的功能?

> e = Error('test')
> console.log(e.toString())
Error: test
> console.log('' + e)
Error: test

You have to access the stack property of the error instance:您必须访问错误实例的stack属性:

 const foo = () => { bar(); }; const bar = () => { const error = new Error('some error'); console.log(error.stack); }; foo();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM