Skip to content

Commit 67efe1f

Browse files
author
Nitzan Uziely
committed
events: change EventTarget handler exception behavior
Change the behavior of EventTarget, instead of emitting 'error' on handler exception, emit 'uncaughtException'. Fixes: nodejs#36770
1 parent cf5f6af commit 67efe1f

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
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

0 commit comments

Comments
 (0)