From ebcba1205dbf9b0652180c2af67c4723db6d2561 Mon Sep 17 00:00:00 2001 From: Patrick Mooney Date: Fri, 25 Oct 2013 22:29:17 -0500 Subject: [PATCH] Fix unhandled error during client connection Client actions utilizing an EventEmitter may generate unhandled errors, especially during initial connection. Tracking whether the emitter has been sent via callback is critical for proper routing of such errors. Fix mcavage/node-ldapjs#144 --- lib/client/client.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/client/client.js b/lib/client/client.js index a90ed19..199ba34 100644 --- a/lib/client/client.js +++ b/lib/client/client.js @@ -757,14 +757,20 @@ Client.prototype._send = function _send(message, expect, emitter, callback) { var log = this.log; var self = this; var timer = false; + var sentEmitter = false; if (!conn) return callback(new ConnectionError('no socket')); function _done(event, obj) { if (emitter) { - if (event === 'error') + if (event === 'error') { + // Error will go unhandled if emitter hasn't been sent via callback. + // Execute callback with the error instead. + if (!sentEmitter) + return callback(obj); emitter.removeAllListeners('end'); + } if (event === 'end') emitter.removeAllListeners('error'); @@ -878,6 +884,7 @@ Client.prototype._send = function _send(message, expect, emitter, callback) { conn.unbindMessageID = message.id; conn.end(); } else if (emitter) { + sentEmitter = true; return callback(null, emitter); } return false;