0

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
user12341234
  • 1,137
  • 2
  • 10
  • 22

1 Answers1

2

You have to access the stack property of the error instance:

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

foo();
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320