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
3

1 Answer 1

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();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.