diff --git a/lib/commands/fetch.js b/lib/commands/fetch.js index ad1cf31..45da8dc 100644 --- a/lib/commands/fetch.js +++ b/lib/commands/fetch.js @@ -203,7 +203,7 @@ module.exports = async (connection, range, query, options) => { } connection.log.warn({ err, cid: connection.id }); - return false; + throw err; } } }; diff --git a/lib/imap-flow.js b/lib/imap-flow.js index 9c8a7d0..9804560 100644 --- a/lib/imap-flow.js +++ b/lib/imap-flow.js @@ -2254,93 +2254,94 @@ class ImapFlow extends EventEmitter { */ async *fetch(range, query, options) { options = options || {}; - try { - if (!this.mailbox) { - // no mailbox selected, nothing to do - return; - } - range = await this.resolveRange(range, options); - if (!range) { - return false; - } + if (!this.mailbox) { + // no mailbox selected, nothing to do + return; + } - let finished = false; - let push = false; - let rowQueue = []; - let getNext = () => - new Promise((resolve, reject) => { - let check = () => { - if (rowQueue.length) { - let entry = rowQueue.shift(); - if (entry.err) { - return reject(entry.err); - } else { - return resolve(entry.value); - } - } - if (finished) { - return resolve(null); - } + range = await this.resolveRange(range, options); + if (!range) { + return false; + } - // wait until data is pushed to queue and try again - push = () => { - push = false; - check(); - }; - }; - check(); - }); + let finished = false; + let push = false; + let rowQueue = []; - this.run('FETCH', range, query, { - uid: !!options.uid, - binary: options.binary, - changedSince: options.changedSince, - onUntaggedFetch: (untagged, next) => { - rowQueue.push({ - value: { - response: untagged, - next + let getNext = () => + new Promise((resolve, reject) => { + let check = () => { + if (rowQueue.length) { + let entry = rowQueue.shift(); + if (entry.err) { + return reject(entry.err); + } else { + return resolve(entry.value); } - }); - if (typeof push === 'function') { - push(); } - } - }) - .then(() => { - finished = true; - if (typeof push === 'function') { - push(); + + if (finished) { + return resolve(null); } - }) - .catch(err => { - rowQueue.push({ err }); - }); - let res; - while ((res = await getNext())) { - if (this.isClosed || this.socket.destroyed) { - let error = new Error('Connection closed'); - error.code = 'EConnectionClosed'; - throw error; - } + // wait until data is pushed to queue and try again + push = () => { + push = false; + check(); + }; + }; + check(); + }); - if (res !== null) { - yield res.response; - res.next(); + this.run('FETCH', range, query, { + uid: !!options.uid, + binary: options.binary, + changedSince: options.changedSince, + onUntaggedFetch: (untagged, next) => { + rowQueue.push({ + value: { + response: untagged, + next + } + }); + if (typeof push === 'function') { + push(); } } + }) + .then(() => { + finished = true; + if (typeof push === 'function') { + push(); + } + }) + .catch(err => { + rowQueue.push({ err }); + if (typeof push === 'function') { + push(); + } + }); - if (!finished) { - // FETCH never finished! - let error = new Error('FETCH did not finish'); - error.code = 'ENotFinished'; + let res; + while ((res = await getNext())) { + if (this.isClosed || this.socket.destroyed) { + let error = new Error('Connection closed'); + error.code = 'EConnectionClosed'; throw error; } - } catch (err) { - setImmediate(() => this.close()); - throw err; + + if (res !== null) { + yield res.response; + res.next(); + } + } + + if (!finished) { + // FETCH never finished! + let error = new Error('FETCH did not finish'); + error.code = 'ENotFinished'; + throw error; } }