From 05091a4447f85c8279da016a02f0318642525a0d Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Fri, 1 Jul 2016 10:24:39 -0700 Subject: [PATCH] Update thrown errors to use serializable NylasError --- packages/nylas-core/imap-connection.js | 32 +++++++++---------- .../imap/fetch-messages-in-category.js | 2 +- packages/nylas-sync/sync-worker.js | 4 +-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/nylas-core/imap-connection.js b/packages/nylas-core/imap-connection.js index 0626e7d4e..c69ecb18f 100644 --- a/packages/nylas-core/imap-connection.js +++ b/packages/nylas-core/imap-connection.js @@ -36,10 +36,10 @@ class IMAPBox { if (range.length === 0) { return Rx.Observable.empty() } - if (!options) { - throw new Error("IMAPBox.fetch now requires an options object.") - } return Rx.Observable.create((observer) => { + if (!options) { + return observer.onError(new NylasError("IMAPBox.fetch now requires an options object.")) + } const f = this._imap.fetch(range, options); f.on('message', (imapMessage) => { const parts = {}; @@ -75,10 +75,10 @@ class IMAPBox { fetchStream({uid, options}) { if (!uid) { - throw new Error("IMAPConnection.fetchStream requires a message uid.") + throw new NylasError("IMAPConnection.fetchStream requires a message uid.") } if (!options) { - throw new Error("IMAPConnection.fetchStream requires an options object.") + throw new NylasError("IMAPConnection.fetchStream requires an options object.") } return new Promise((resolve, reject) => { const f = this._imap.fetch(uid, options); @@ -123,28 +123,28 @@ class IMAPBox { addFlags(range, flags) { if (!this._imap) { - throw new Error(`IMAPBox::addFlags - You need to call connect() first.`) + throw new NylasError(`IMAPBox::addFlags - You need to call connect() first.`) } return this._imap.addFlagsAsync(range, flags) } delFlags(range, flags) { if (!this._imap) { - throw new Error(`IMAPBox::delFlags - You need to call connect() first.`) + throw new NylasError(`IMAPBox::delFlags - You need to call connect() first.`) } return this._imap.delFlagsAsync(range, flags) } moveFromBox(range, folderName) { if (!this._imap) { - throw new Error(`IMAPBox::moveFromBox - You need to call connect() first.`) + throw new NylasError(`IMAPBox::moveFromBox - You need to call connect() first.`) } return this._imap.moveAsync(range, folderName) } closeBox({expunge = true} = {}) { if (!this._imap) { - throw new Error(`IMAPBox::closeBox - You need to call connect() first.`) + throw new NylasError(`IMAPBox::closeBox - You need to call connect() first.`) } return this._imap.closeBoxAsync(expunge) } @@ -259,7 +259,7 @@ class IMAPConnection extends EventEmitter { serverSupports(capability) { if (!this._imap) { - throw new Error(`IMAPConnection::serverSupports - You need to call connect() first.`) + throw new NylasError(`IMAPConnection::serverSupports - You need to call connect() first.`) } this._imap.serverSupports(capability); } @@ -269,7 +269,7 @@ class IMAPConnection extends EventEmitter { */ openBox(folderName, {readOnly = false} = {}) { if (!this._imap) { - throw new Error(`IMAPConnection::openBox - You need to call connect() first.`) + throw new NylasError(`IMAPConnection::openBox - You need to call connect() first.`) } return this._imap.openBoxAsync(folderName, readOnly).then((box) => new IMAPBox(this._imap, box) @@ -278,35 +278,35 @@ class IMAPConnection extends EventEmitter { getBoxes() { if (!this._imap) { - throw new Error(`IMAPConnection::getBoxes - You need to call connect() first.`) + throw new NylasError(`IMAPConnection::getBoxes - You need to call connect() first.`) } return this._imap.getBoxesAsync() } addBox(folderName) { if (!this._imap) { - throw new Error(`IMAPConnection::addBox - You need to call connect() first.`) + throw new NylasError(`IMAPConnection::addBox - You need to call connect() first.`) } return this._imap.addBoxAsync(folderName) } renameBox(oldFolderName, newFolderName) { if (!this._imap) { - throw new Error(`IMAPConnection::renameBox - You need to call connect() first.`) + throw new NylasError(`IMAPConnection::renameBox - You need to call connect() first.`) } return this._imap.renameBoxAsync(oldFolderName, newFolderName) } delBox(folderName) { if (!this._imap) { - throw new Error(`IMAPConnection::delBox - You need to call connect() first.`) + throw new NylasError(`IMAPConnection::delBox - You need to call connect() first.`) } return this._imap.delBoxAsync(folderName) } runOperation(operation) { if (!this._imap) { - throw new Error(`IMAPConnection::runOperation - You need to call connect() first.`) + throw new NylasError(`IMAPConnection::runOperation - You need to call connect() first.`) } return new Promise((resolve, reject) => { this._queue.push({operation, resolve, reject}); diff --git a/packages/nylas-sync/imap/fetch-messages-in-category.js b/packages/nylas-sync/imap/fetch-messages-in-category.js index cf70dcef0..4b79042cf 100644 --- a/packages/nylas-sync/imap/fetch-messages-in-category.js +++ b/packages/nylas-sync/imap/fetch-messages-in-category.js @@ -329,7 +329,7 @@ class FetchMessagesInFolder { _runScan() { const {fetchedmin, fetchedmax} = this._category.syncState; if (!fetchedmin || !fetchedmax) { - throw new Error("Unseen messages must be fetched at least once before the first update/delete scan.") + throw new NylasError("Unseen messages must be fetched at least once before the first update/delete scan.") } return this._shouldRunDeepScan() ? this._runDeepScan() : this._runShallowScan() } diff --git a/packages/nylas-sync/sync-worker.js b/packages/nylas-sync/sync-worker.js index 915c10e25..d893ce764 100644 --- a/packages/nylas-sync/sync-worker.js +++ b/packages/nylas-sync/sync-worker.js @@ -47,7 +47,7 @@ class SyncWorker { case MessageTypes.SYNCBACK_REQUESTED: this.syncNow(); break; default: - throw new Error(`Invalid message: ${msg}`) + throw new NylasError(`Invalid message: ${msg}`) } } @@ -197,7 +197,7 @@ class SyncWorker { return Promise.resolve() } - throw new Error(`SyncWorker.onSyncDidComplete: Unknown afterSync behavior: ${afterSync}. Closing connection`) + throw new NylasError(`SyncWorker.onSyncDidComplete: Unknown afterSync behavior: ${afterSync}. Closing connection`) } scheduleNextSync() {