Skip to content

Commit

Permalink
domain,events: support non-object 'error' argument
Browse files Browse the repository at this point in the history
Fix a TypeError when emitting an 'error' argument with a non-object
argument (like a string) when domains are active.

Fixes: nodejs/help#501
PR-URL: #11438
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
bnoordhuis committed Feb 22, 2017
1 parent 3f02b47 commit 4f638f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ EventEmitter.prototype.emit = function emit(type) {
if (domain) {
if (!er)
er = new Error('Uncaught, unspecified "error" event');
er.domainEmitter = this;
er.domain = domain;
er.domainThrown = false;
if (typeof er === 'object' && er !== null) {
er.domainEmitter = this;
er.domain = domain;
er.domainThrown = false;
}
domain.emit('error', er);
} else if (er instanceof Error) {
throw er; // Unhandled 'error' event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@ const common = require('../common');
const assert = require('assert');
const events = require('events');
const domain = require('domain');
const e = new events.EventEmitter();

const d = domain.create();
d.add(e);
d.on('error', common.mustCall(function(er) {
assert(er instanceof Error, 'error created');
}));
{
const e = new events.EventEmitter();
const d = domain.create();
d.add(e);
d.on('error', common.mustCall(function(er) {
assert(er instanceof Error, 'error created');
}));
e.emit('error');
}

e.emit('error');
for (const arg of [false, null, undefined]) {
const e = new events.EventEmitter();
const d = domain.create();
d.add(e);
d.on('error', common.mustCall(function(er) {
assert(er instanceof Error, 'error created');
}));
e.emit('error', arg);
}

for (const arg of [42, 'fortytwo', true]) {
const e = new events.EventEmitter();
const d = domain.create();
d.add(e);
d.on('error', common.mustCall(function(er) {
assert.strictEqual(er, arg);
}));
e.emit('error', arg);
}

0 comments on commit 4f638f6

Please sign in to comment.