From 29174e42e4be8b892e1f25df65720cc23c768bb2 Mon Sep 17 00:00:00 2001 From: Brian White Date: Thu, 28 Jun 2012 16:09:47 -0400 Subject: [PATCH] Fix connection-level error propagation --- imap.js | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/imap.js b/imap.js index 0c9a38f..96e3ea8 100644 --- a/imap.js +++ b/imap.js @@ -128,9 +128,27 @@ ImapConnection.prototype.connect = function(loginCb) { self._state.conn.cleartext.write(''); self._state.status = STATES.NOAUTH; }); - this._state.conn.on('ready', function() { - fnInit(); + this._state.conn.on('end', function() { + self._reset(); + self.debug('FIN packet received. Disconnecting...'); + self.emit('end'); + }); + function errorHandler(err) { + clearTimeout(self._state.tmrConn); + if (self._state.status === STATES.NOCONNECT) + loginCb(new Error('Unable to connect. Reason: ' + err)); + self.emit('error', err); + self.debug('Error occurred: ' + err); + } + //this._state.conn.on('error', errorHandler); + this._state.conn.cleartext.on('error', errorHandler); + this._state.conn.on('close', function(had_error) { + self._reset(); + self.debug('Connection forcefully closed.'); + self.emit('close', had_error); }); + this._state.conn.on('ready', fnInit); + this._state.conn.cleartext.on('data', function(data) { if (data.length === 0) return; var trailingCRLF = false, literalInfo; @@ -511,23 +529,6 @@ ImapConnection.prototype.connect = function(loginCb) { // unknown response } }); - this._state.conn.on('end', function() { - self._reset(); - self.debug('FIN packet received. Disconnecting...'); - self.emit('end'); - }); - this._state.conn.on('error', function(err) { - clearTimeout(self._state.tmrConn); - if (self._state.status === STATES.NOCONNECT) - loginCb(new Error('Unable to connect. Reason: ' + err)); - self.emit('error', err); - self.debug('Error occurred: ' + err); - }); - this._state.conn.on('close', function(had_error) { - self._reset(); - self.debug('Connection forcefully closed.'); - self.emit('close', had_error); - }); this._state.conn.connect(this._options.port, this._options.host); this._state.tmrConn = setTimeout(this._fnTmrConn.bind(this),