-1

When node encounters an caught exception, it prints the line the error occurred on, followed by the stack trace, then exits:

/example.js:1
throw new Error()
^

Error
    at ...

I would like to catch an exception, print exactly the same thing, and continue. Basically:

try {
  something();
} catch (e) {
  // what goes here?
  console.error(e);
}

Note that the above example already prints the stack trace. The missing part is the first four lines, including the file/line reference, line text, and column indicator.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91

1 Answers1

-1

You can use the stack property of Error:

try {
  something();
} catch (e) {
  console.err(e.stack);
}
mgarcia
  • 5,669
  • 3
  • 16
  • 35
  • This is functionally almost identical to the example code in the question. It does not print the entire specified output (what node prints for uncaught exceptions). – Thom Smith Aug 27 '19 at 18:58