In the event the implemented error handling isn’t what it’s all cracked up to be, having the Node.js debugger break on uncaught exceptions is quite simple:
1 2 3 |
if ( global.v8debug ) { global.v8debug.Debug.setBreakOnException(); // speaks for itself } |
Assume the following code, where bogus is undefined:
1 2 3 |
setTimeout(function() { bogus(); // "`bogus` is not defined" },2000); |
Line 3 will terminate the application (normally), and log an exception in stdout (or the console, a log, etc.) — even when Node.js is in debug mode.
To debug this uncaught exception JIT, the implementation may be as follows:
1 2 3 4 5 6 |
if ( global.v8debug) { global.v8debug.Debug.setBreakOnException(); // enable it, global.v8debug is only defined when the --debug or --debug-brk flag is set } setTimeout(function() { bogus(); // should break here, "`bogus` is not defined" },2000); |
Run the above application (whose filename is “index.js”) as node --debug index.js and after 2 seconds (notice the timeout) the exception will be uncaught, but the process will break on line 5 for debugging.