31

In node.js if you catch uncaughtExceptions, like so:

process.on('uncaughtException', function (error) {
  console.log(error);
});

The error message displayed doesn't contain all the information that you receive if you don't catch the error and just let the process crash. When you let the process crash it includes what line caused the error. Is there any way to get the full error message including the line that caused the error so we can log this data using uncaughtException.

Sean Bannister
  • 3,105
  • 4
  • 31
  • 43

2 Answers2

65

Try error.stack

process.on('uncaughtException', function (error) {
   console.log(error.stack);
});
mike
  • 7,137
  • 2
  • 23
  • 27
  • 3
    How to show more, because in my case, error.stack just show 3 row, I can not trace the root of issue raising the error – John Nguyen Mar 06 '15 at 02:30
  • upvoted! isnt error.stack a nonstandard extension https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error or does node have it everywhere – PirateApp Oct 14 '18 at 14:03
7

Try:

process.on('uncaughtException', function (error) {
   console.dir(error);
});
TheHippo
  • 61,720
  • 15
  • 75
  • 100
  • 1
    Sir, Any difference between the question and the answer? – Jayram Jun 13 '14 at 09:14
  • 1
    @Jayram Yes. The question used `console.log(error)` and I suggestion using `console.dir(error)`. Different functions on the `console` object. – TheHippo Jun 13 '14 at 13:03