Skip to content

Commit

Permalink
fixup! fixup! event: simplify domain conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Trott committed Mar 31, 2021
1 parent 77fd57d commit f992e36
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
68 changes: 34 additions & 34 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,49 +335,49 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
} else if (!doError)
return false;

const domain = this.domain;

// If there is no 'error' event listener then throw.
if (doError) {
let er;
if (args.length > 0)
er = args[0];
if (domain) {
if (!this.domain) {
if (er instanceof Error) {
try {
const capture = {};
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
value: enhanceStackTrace.bind(this, er, capture),
configurable: true
});
} catch {}

// Note: The comments on the `throw` lines are intentional, they show
// up in Node's output if this results in an unhandled exception.
throw er; // Unhandled 'error' event
} else {
let stringifiedEr;
const { inspect } = require('internal/util/inspect');
try {
stringifiedEr = inspect(er);
} catch {
stringifiedEr = er;
}

// At least give some kind of context to the user
const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
err.context = er;
throw err; // Unhandled 'error' event
}
} else {
if (!er) {
er = new ERR_UNHANDLED_ERROR();
}
if (typeof er === 'object') {
er.domainEmitter = this;
er.domain = domain;
er.domain = this.domain;
er.domainThrown = false;
}
domain.emit('error', er);
} else if (er instanceof Error) {
try {
const capture = {};
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
value: enhanceStackTrace.bind(this, er, capture),
configurable: true
});
} catch {}

// Note: The comments on the `throw` lines are intentional, they show
// up in Node's output if this results in an unhandled exception.
throw er; // Unhandled 'error' event
} else {
let stringifiedEr;
const { inspect } = require('internal/util/inspect');
try {
stringifiedEr = inspect(er);
} catch {
stringifiedEr = er;
}

// At least give some kind of context to the user
const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
err.context = er;
throw err; // Unhandled 'error' event
this.domain.emit('error', er);
}

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

let needDomainExit = false;
if (domain && this !== process) {
domain.enter();
if (this.domain && this !== process) {
this.domain.enter();
needDomainExit = true;
}

Expand Down Expand Up @@ -421,7 +421,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
}

if (needDomainExit)
domain.exit();
this.domain.exit();

return true;
};
Expand Down
4 changes: 2 additions & 2 deletions test/message/events_unhandled_error_common_trace.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node:events:*
throw er; // Unhandled 'error' event
^
throw er; // Unhandled 'error' event
^

Error: foo:bar
at bar (*events_unhandled_error_common_trace.js:*:*)
Expand Down
4 changes: 2 additions & 2 deletions test/message/events_unhandled_error_nexttick.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node:events:*
throw er; // Unhandled 'error' event
^
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (*events_unhandled_error_nexttick.js:*:*)
Expand Down
4 changes: 2 additions & 2 deletions test/message/events_unhandled_error_sameline.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node:events:*
throw er; // Unhandled 'error' event
^
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (*events_unhandled_error_sameline.js:*:*)
Expand Down
4 changes: 2 additions & 2 deletions test/message/events_unhandled_error_subclass.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node:events:*
throw er; // Unhandled 'error' event
^
throw er; // Unhandled 'error' event
^

Error
at Object.<anonymous> (*events_unhandled_error_subclass.js:*:*)
Expand Down

0 comments on commit f992e36

Please sign in to comment.