events: change EventTarget handler exception behavior · nodejs/node@83d6e63 · GitHub
Skip to content

Commit 83d6e63

Browse files
Nitzan Uzielybenjamingr
authored andcommitted
events: change EventTarget handler exception behavior
Change the behavior of EventTarget, instead of emitting 'error' on handler exception, emit 'uncaughtException'. Fixes: #36770 PR-URL: #37237 Fixes: #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

4 files changed

Lines changed: 23 additions & 18 deletions

File tree

doc/api/events.md

Lines changed: 3 additions & 0 deletions

lib/internal/event_target.js

Lines changed: 6 additions & 6 deletions
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 7 additions & 7 deletions

0 commit comments

Comments
 (0)