Missing stack trace on node.js uncaughtException thrown by throw ()

I am trying to catch a stack trace of a node.js uncaughtException and works fine for various errors, but not for throw () statements:

Fix stack trace when handling exceptions:

$ cat errorFunc.js process.on('uncaughtException', function(exception) { console.log('uncaughtException occurred: ' + exception.stack); }); MyError(); $ node errorFunc.js uncaughtException occurred: ReferenceError: MyError is not defined at Object.<anonymous> (/home/jolcese/code/WebEnclaves/Server/errorFunc.js:5:1) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3 $ 

No stack trace on exception thrown by throw ():

 $ cat errorThrow.js process.on('uncaughtException', function(exception) { console.log('uncaughtException occurred: ' + exception.stack); }); throw('my error'); $ node errorThrow.js uncaughtException occurred: undefined $ 

Any idea why?

Thanks jose

Disclaimer: I know that using process.on ('uncaughtException') is very, very bad, and I will be punished, but using domains in this code is not an option.

+7
source share
1 answer

JavaScript allows you to throw something.

If you want to throw stacked trace errors in JavaScript, you need to throw Error objects. (specification)

In addition, throw is an operator, not a function.

Try

 throw new Error('my error'); 

See the Mozilla Developer Network guide for more information.

+7
source

All Articles