diff options
author | Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> | 2019-12-12 23:08:42 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-12-20 03:42:51 +0300 |
commit | fcd4c2e37d510ea362b4225849b25ac958390091 (patch) | |
tree | 4aa1ae6de4c76912ab56395dbeb1ce060d22a79f /lib | |
parent | 20d009d2fdab313bb2cfb789881f1f9364474a62 (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 'lib')
-rw-r--r-- | lib/events.js | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/events.js b/lib/events.js index fc530ff7f3f..c91792cbfbd 100644 --- a/lib/events.js +++ b/lib/events.js @@ -55,6 +55,7 @@ const { } = require('internal/util/inspect'); const kCapture = Symbol('kCapture'); +const kErrorMonitor = Symbol('events.errorMonitor'); function EventEmitter(opts) { EventEmitter.init.call(this, opts); @@ -83,6 +84,13 @@ ObjectDefineProperty(EventEmitter, 'captureRejections', { enumerable: true }); +ObjectDefineProperty(EventEmitter, 'errorMonitor', { + value: kErrorMonitor, + writable: false, + configurable: true, + enumerable: true +}); + // The default for captureRejections is false ObjectDefineProperty(EventEmitter.prototype, kCapture, { value: false, @@ -256,9 +264,11 @@ EventEmitter.prototype.emit = function emit(type, ...args) { let doError = (type === 'error'); const events = this._events; - if (events !== undefined) + if (events !== undefined) { + if (doError && events[kErrorMonitor] !== undefined) + this.emit(kErrorMonitor, ...args); doError = (doError && events.error === undefined); - else if (!doError) + } else if (!doError) return false; // If there is no 'error' event listener then throw. |