Skip to content

Commit 83d6e63

Browse files
Nitzan Uzielybenjamingr
Nitzan Uziely
authored andcommitted
events: change EventTarget handler exception behavior
Change the behavior of EventTarget, instead of emitting 'error' on handler exception, emit 'uncaughtException'. Fixes: nodejs#36770 PR-URL: nodejs#37237 Fixes: nodejs#36770 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 9803729 commit 83d6e63

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

doc/api/events.md

+3
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@ setMaxListeners(5, target, emitter);
11371137
<!-- YAML
11381138
added: v14.5.0
11391139
changes:
1140+
- version: REPLACEME
1141+
pr-url: https://github.com/nodejs/node/pull/37237
1142+
description: changed EventTarget error handling.
11401143
- version: v15.4.0
11411144
pr-url: https://github.com/nodejs/node/pull/35949
11421145
description: No longer experimental.

lib/internal/event_target.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,9 @@ class EventTarget {
459459
result = FunctionPrototypeCall(callback, this, arg);
460460
}
461461
if (result !== undefined && result !== null)
462-
addCatch(this, result, createEvent());
462+
addCatch(result);
463463
} catch (err) {
464-
emitUnhandledRejectionOrErr(this, err, createEvent());
464+
emitUncaughtException(err);
465465
}
466466

467467
handler = next;
@@ -624,19 +624,19 @@ function isEventTarget(obj) {
624624
return obj?.constructor?.[kIsEventTarget];
625625
}
626626

627-
function addCatch(that, promise, event) {
627+
function addCatch(promise) {
628628
const then = promise.then;
629629
if (typeof then === 'function') {
630630
FunctionPrototypeCall(then, promise, undefined, function(err) {
631631
// The callback is called with nextTick to avoid a follow-up
632632
// rejection from this promise.
633-
process.nextTick(emitUnhandledRejectionOrErr, that, err, event);
633+
emitUncaughtException(err);
634634
});
635635
}
636636
}
637637

638-
function emitUnhandledRejectionOrErr(that, err, event) {
639-
process.emit('error', err, event);
638+
function emitUncaughtException(err) {
639+
process.nextTick(() => { throw err; });
640640
}
641641

642642
function makeEventHandler(handler) {

test/parallel/test-eventtarget.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,16 @@ let asyncTest = Promise.resolve();
178178
}
179179

180180
{
181-
const uncaughtException = common.mustCall((err, event) => {
181+
const uncaughtException = common.mustCall((err, origin) => {
182182
strictEqual(err.message, 'boom');
183-
strictEqual(event.type, 'foo');
183+
strictEqual(origin, 'uncaughtException');
184184
}, 4);
185185

186-
// Whether or not the handler function is async or not, errors
187-
// are routed to uncaughtException
188-
process.on('error', uncaughtException);
186+
// Make sure that we no longer call 'error' on error.
187+
process.on('error', common.mustNotCall());
188+
// Don't call rejection even for async handlers.
189+
process.on('unhandledRejection', common.mustNotCall());
190+
process.on('uncaughtException', uncaughtException);
189191

190192
const eventTarget = new EventTarget();
191193

test/parallel/test-process-uncaught-exception-monitor.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ process.on(
5555
process.on('uncaughtException', common.mustCall((err, origin) => {
5656
assert.strictEqual(origin, 'uncaughtException');
5757
assert.strictEqual(err, theErr);
58-
}));
5958

60-
process.nextTick(common.mustCall(() => {
61-
// Test with uncaughtExceptionCaptureCallback installed
62-
process.setUncaughtExceptionCaptureCallback(common.mustCall(
63-
(err) => assert.strictEqual(err, theErr))
64-
);
59+
process.nextTick(common.mustCall(() => {
60+
// Test with uncaughtExceptionCaptureCallback installed
61+
process.setUncaughtExceptionCaptureCallback(common.mustCall(
62+
(err) => assert.strictEqual(err, theErr))
63+
);
6564

66-
throw theErr;
65+
throw theErr;
66+
}));
6767
}));
6868

6969
throw theErr;

0 commit comments

Comments
 (0)