1

I had some problematic code in a Node.js application which caused an error (manifested as strange output) but not a total crash. It displayed nothing in the console to indicate an error occurred, and I only identified the root cause by a lot of trial and error in commenting out lines selectively.

To save time in future, is there anything like Java's UncaughtExceptionHandler in Node that will catch anything that's causing errors and display them in the console so I can pinpoint bug(s) immediately?

HomerPlata
  • 1,687
  • 5
  • 22
  • 39
  • It sounds like a logic error in your code, otherwise you would have gotten an error being shown on console. – robertklep Mar 21 '17 at 13:44

1 Answers1

3

Yes. You can listen for that event by doing this

process.on('uncaughtException', (err) => {

});

It will override the default behaviour of exiting.

Documentation

Ryan
  • 1,863
  • 13
  • 20
  • That would catch exceptions that otherwise would be shown on console _and_ crash the app, both of which didn't happen. – robertklep Mar 21 '17 at 13:43
  • Yes, you are right actually. He now knows about this if ever needed. – Ryan Mar 21 '17 at 13:45
  • Thanks Ryan and Robert. Yes, it didn't actually help solve the problem I'm having but it did technically answer what I asked. Accepted :-) I will edit the question with an update on what the actual problem was. – HomerPlata Mar 21 '17 at 14:00