diff options
author | Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> | 2019-12-12 23:08:42 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2020-01-03 18:12:06 +0300 |
commit | f570de8ea9893ca424621ed22fbf8a2dfe8493e3 (patch) | |
tree | 6b1cc5e41f899f2b4eead419f0c52585207a3409 /doc/api | |
parent | 6b65cafacf2344f541956a2eaf58611b2eb106be (diff) |
events: allow monitoring error events
Installing an error listener has a side effect that emitted errors are
considered as handled. This is quite bad for monitoring/logging tools
which tend to be interested in errors but don't want to cause side
effects like swallow an exception.
There are some workarounds in the wild like monkey patching emit or
remit the error if monitoring tool detects that it is the only listener
but this is error prone and risky.
This PR allows to install a listener to monitor errors with the side
effect to consume the error. To avoid conflicts with other events it
exports a symbol on EventEmitter which owns this special meaning.
Refs: https://github.com/open-telemetry/opentelemetry-js/issues/225
PR-URL: https://github.com/nodejs/node/pull/30932
Refs: https://github.com/open-telemetry/opentelemetry-js/issues/225
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'doc/api')
-rw-r--r-- | doc/api/events.md | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/doc/api/events.md b/doc/api/events.md index 434d8de8b57..69f309a73bc 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -155,6 +155,18 @@ myEmitter.emit('error', new Error('whoops!')); // Prints: whoops! there was an error ``` +It is possible to monitor `'error'` events without consuming the emitted error +by installing a listener using the symbol `errorMonitor`. + +```js +const myEmitter = new MyEmitter(); +myEmitter.on(EventEmitter.errorMonitor, (err) => { + MyMonitoringTool.log(err); +}); +myEmitter.emit('error', new Error('whoops!')); +// Still throws and crashes Node.js +``` + ## Capture Rejections of Promises > Stability: 1 - captureRejections is experimental. @@ -348,6 +360,19 @@ the event emitter instance, the event’s name and the number of attached listeners, respectively. Its `name` property is set to `'MaxListenersExceededWarning'`. +### EventEmitter.errorMonitor +<!-- YAML +added: REPLACEME +--> + +This symbol shall be used to install a listener for only monitoring `'error'` +events. Listeners installed using this symbol are called before the regular +`'error'` listeners are called. + +Installing a listener using this symbol does not change the behavior once an +`'error'` event is emitted, therefore the process will still crash if no +regular `'error'` listener is installed. + ### emitter.addListener(eventName, listener) <!-- YAML added: v0.1.26 |