lundi 30 mars 2015

Catch all `error` events from any EventEmitter in node

Via the Node.js documentation, an unhandled EventEmitter will crash a running process:



When an EventEmitter instance experiences an error, the typical action is to emit an 'error' event. Error events are treated as a special case in node. If there is no listener for it, then the default action is to print a stack trace and exit the program.



I would very much like for the process to not crash when this happens.[1] Ideally, I could catch every instance of an EventEmitter error like this:



emitter.on('error', function(err) { console.log(err); })


However our application is large, a simple search of the node_modules folder reveals that there are lots of EventEmitters, and tracking them down would be cumbersome.


Is there a global hook I can use to catch all instances of an EventEmitter failure?


I tried process.on('uncaughtException') but this doesn't catch EventEmitter errors. I also tried process.on('error') which catches errors emitted by the process, but does not catch errors emitted by other EventEmitters.


Other places say you should use domains, however, it sounds like you need to wrap specific function calls in it, at which point you might as well find and wrap every EventEmitter with .on('error'). My colleague also says domains are, if not deprecated, not going to be used going forward.


[1] I understand the logic behind the "processes should crash" argument. Partly I would like to keep processes alive because a) our server takes a long time to restart, and b) processes keep crashing with literally zero stack trace; I figure keeping the process alive will help with logging and tracking down errors.


Aucun commentaire:

Enregistrer un commentaire