diff --git a/lib/Connection.js b/lib/Connection.js index 292d854e..6fcf5cca 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -193,8 +193,16 @@ Connection.prototype.connect = function() { self._resTagged(info); }); parser.on('body', function(stream, info) { - var msg = self._curReq.fetchCache[info.seqno], toget; - + if (!self._curReq) { + stream.resume(); + return; + } + var msg = undefined; + var toget; + if (self._curReq.fetchCache.hasOwnProperty(info.seqno)) { + msg = self._curReq.fetchCache[info.seqno]; + } + if (msg === undefined) { msg = self._curReq.fetchCache[info.seqno] = { msgEmitter: new EventEmitter(), @@ -422,9 +430,11 @@ Connection.prototype.openBox = function(name, readOnly, cb) { if (err) { self._box = undefined; cb(err); - } else { + } else if (self._box) { self._box.name = name; cb(err, self._box); + } else { + cb(new Error('No mailbox is currently selected')); } }); }; @@ -1400,7 +1410,7 @@ Connection.prototype._resUntagged = function(info) { } this._curReq.cbargs.push(box); } else if (type === 'fetch') { - if (/^(?:UID )?FETCH/.test(this._curReq.fullcmd)) { + if (this._curReq && /^(?:UID )?FETCH/.test(this._curReq.fullcmd)) { // FETCH response sent as result of FETCH request var msg = this._curReq.fetchCache[info.num], keys = Object.keys(info.text),