-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: open-telemetry/opentelemetry-js#225 PR-URL: #30932 Refs: open-telemetry/opentelemetry-js#225 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const EventEmitter = require('events'); | ||
|
||
const EE = new EventEmitter(); | ||
const theErr = new Error('MyError'); | ||
|
||
EE.on( | ||
EventEmitter.errorMonitor, | ||
common.mustCall(function onErrorMonitor(e) { | ||
assert.strictEqual(e, theErr); | ||
}, 3) | ||
); | ||
|
||
// Verify with no error listener | ||
common.expectsError( | ||
() => EE.emit('error', theErr), theErr | ||
); | ||
|
||
// Verify with error listener | ||
EE.once('error', common.mustCall((e) => assert.strictEqual(e, theErr))); | ||
EE.emit('error', theErr); | ||
|
||
|
||
// Verify it works with once | ||
process.nextTick(() => EE.emit('error', theErr)); | ||
assert.rejects(EventEmitter.once(EE, 'notTriggered'), theErr); | ||
|
||
// Only error events trigger error monitor | ||
EE.on('aEvent', common.mustCall()); | ||
EE.emit('aEvent'); |