event-custodian
Control handlers for an event set on an EventEmitter
Usage
TL;DR
const Custodian = ;emitter 'event'; // Example: Avoid errors in events that can cause the process to exit with SIGTERMprocess 'unhandledRejection';
Motivation
By overriding native behaviour we can verify existing event handlers run in a safe environment, within a try/catch block. This way we can avoid unexpected results, such as the process exiting unexpectedly within an event handler. We can later decide how we want to handle these errors by placing a general onerror handler on the custodian.
Detailed example using process and "unhandledRejection"
// Reduce all existing listeners to oneconst custodian = process 'unhandledRejection'; // Reduce all existing listeners to onecustodian; // Handle errors coming up from registered handlerscustodian; // Add, prepend, remove event handlers as normalprocess ; // Custodian is now managing the call stack // Revert to native subscription functions (remove override). Reinstate all existing handlers as individual event handlerscustodian;
Important note about 'unhandledRejection'
If you use this application to manage unhandledRejection
, you must set an on('error')
handler. Otherwise the custodian will simply print the errors onto console.error
.