Skip to content

Commit f992e36

Browse files
committed
fixup! fixup! event: simplify domain conditions
1 parent 77fd57d commit f992e36

File tree

5 files changed

+42
-42
lines changed

5 files changed

+42
-42
lines changed

lib/events.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -335,49 +335,49 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
335335
} else if (!doError)
336336
return false;
337337

338-
const domain = this.domain;
339-
340338
// If there is no 'error' event listener then throw.
341339
if (doError) {
342340
let er;
343341
if (args.length > 0)
344342
er = args[0];
345-
if (domain) {
343+
if (!this.domain) {
344+
if (er instanceof Error) {
345+
try {
346+
const capture = {};
347+
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
348+
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
349+
value: enhanceStackTrace.bind(this, er, capture),
350+
configurable: true
351+
});
352+
} catch {}
353+
354+
// Note: The comments on the `throw` lines are intentional, they show
355+
// up in Node's output if this results in an unhandled exception.
356+
throw er; // Unhandled 'error' event
357+
} else {
358+
let stringifiedEr;
359+
const { inspect } = require('internal/util/inspect');
360+
try {
361+
stringifiedEr = inspect(er);
362+
} catch {
363+
stringifiedEr = er;
364+
}
365+
366+
// At least give some kind of context to the user
367+
const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
368+
err.context = er;
369+
throw err; // Unhandled 'error' event
370+
}
371+
} else {
346372
if (!er) {
347373
er = new ERR_UNHANDLED_ERROR();
348374
}
349375
if (typeof er === 'object') {
350376
er.domainEmitter = this;
351-
er.domain = domain;
377+
er.domain = this.domain;
352378
er.domainThrown = false;
353379
}
354-
domain.emit('error', er);
355-
} else if (er instanceof Error) {
356-
try {
357-
const capture = {};
358-
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
359-
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
360-
value: enhanceStackTrace.bind(this, er, capture),
361-
configurable: true
362-
});
363-
} catch {}
364-
365-
// Note: The comments on the `throw` lines are intentional, they show
366-
// up in Node's output if this results in an unhandled exception.
367-
throw er; // Unhandled 'error' event
368-
} else {
369-
let stringifiedEr;
370-
const { inspect } = require('internal/util/inspect');
371-
try {
372-
stringifiedEr = inspect(er);
373-
} catch {
374-
stringifiedEr = er;
375-
}
376-
377-
// At least give some kind of context to the user
378-
const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
379-
err.context = er;
380-
throw err; // Unhandled 'error' event
380+
this.domain.emit('error', er);
381381
}
382382

383383
return false;
@@ -389,8 +389,8 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
389389
return false;
390390

391391
let needDomainExit = false;
392-
if (domain && this !== process) {
393-
domain.enter();
392+
if (this.domain && this !== process) {
393+
this.domain.enter();
394394
needDomainExit = true;
395395
}
396396

@@ -421,7 +421,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
421421
}
422422

423423
if (needDomainExit)
424-
domain.exit();
424+
this.domain.exit();
425425

426426
return true;
427427
};

test/message/events_unhandled_error_common_trace.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
3-
^
2+
throw er; // Unhandled 'error' event
3+
^
44

55
Error: foo:bar
66
at bar (*events_unhandled_error_common_trace.js:*:*)

test/message/events_unhandled_error_nexttick.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
3-
^
2+
throw er; // Unhandled 'error' event
3+
^
44

55
Error
66
at Object.<anonymous> (*events_unhandled_error_nexttick.js:*:*)

test/message/events_unhandled_error_sameline.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
3-
^
2+
throw er; // Unhandled 'error' event
3+
^
44

55
Error
66
at Object.<anonymous> (*events_unhandled_error_sameline.js:*:*)

test/message/events_unhandled_error_subclass.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node:events:*
2-
throw er; // Unhandled 'error' event
3-
^
2+
throw er; // Unhandled 'error' event
3+
^
44

55
Error
66
at Object.<anonymous> (*events_unhandled_error_subclass.js:*:*)

0 commit comments

Comments
 (0)