From 4f638f6fd73c0936cfa088ebb1d5c075c9565b85 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 17 Feb 2017 14:01:37 +0100 Subject: [PATCH] domain,events: support non-object 'error' argument Fix a TypeError when emitting an 'error' argument with a non-object argument (like a string) when domains are active. Fixes: https://github.com/nodejs/help/issues/501 PR-URL: https://github.com/nodejs/node/pull/11438 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/events.js | 8 +++-- ...mitter-no-error-provided-to-error-event.js | 35 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/events.js b/lib/events.js index 000fa98d5a..ab167bb2fb 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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 diff --git a/test/parallel/test-event-emitter-no-error-provided-to-error-event.js b/test/parallel/test-event-emitter-no-error-provided-to-error-event.js index 3e7242915e..bc9eac2887 100644 --- a/test/parallel/test-event-emitter-no-error-provided-to-error-event.js +++ b/test/parallel/test-event-emitter-no-error-provided-to-error-event.js @@ -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); +}