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
This commit is contained in:
Patrick Mooney 2013-10-25 22:29:17 -05:00
parent df13275b8e
commit ebcba1205d
1 changed files with 8 additions and 1 deletions

View File

@ -757,14 +757,20 @@ Client.prototype._send = function _send(message, expect, emitter, callback) {
var log = this.log; var log = this.log;
var self = this; var self = this;
var timer = false; var timer = false;
var sentEmitter = false;
if (!conn) if (!conn)
return callback(new ConnectionError('no socket')); return callback(new ConnectionError('no socket'));
function _done(event, obj) { function _done(event, obj) {
if (emitter) { 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'); emitter.removeAllListeners('end');
}
if (event === 'end') if (event === 'end')
emitter.removeAllListeners('error'); emitter.removeAllListeners('error');
@ -878,6 +884,7 @@ Client.prototype._send = function _send(message, expect, emitter, callback) {
conn.unbindMessageID = message.id; conn.unbindMessageID = message.id;
conn.end(); conn.end();
} else if (emitter) { } else if (emitter) {
sentEmitter = true;
return callback(null, emitter); return callback(null, emitter);
} }
return false; return false;