From 6e6012bd78a257880b48483c15e1080561e96eaa Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Fri, 4 Oct 2013 13:15:16 +0200 Subject: [PATCH] cleanup error handling in email dao and mail-list controller --- src/js/controller/mail-list.js | 16 ++++++--- src/js/dao/email-dao.js | 56 +++++++++--------------------- test/integration/email-dao-test.js | 32 +++++++++++++++-- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/src/js/controller/mail-list.js b/src/js/controller/mail-list.js index 36a4f28..b405161 100644 --- a/src/js/controller/mail-list.js +++ b/src/js/controller/mail-list.js @@ -98,7 +98,9 @@ define(function(require) { emailDao.imapLogin(function(err) { if (err) { - console.error(err); + console.log(err); + updateStatus('Error on login!'); + $scope.$apply(); return; } @@ -109,7 +111,9 @@ define(function(require) { function syncImapFolder(options, callback) { emailDao.unreadMessages(getFolder().path, function(err, unreadCount) { if (err) { - console.error(err); + console.log(err); + updateStatus('Error on sync!'); + $scope.$apply(); return; } // set unread count in folder model @@ -118,7 +122,9 @@ define(function(require) { emailDao.imapSync(options, function(err) { if (err) { - console.error(err); + console.log(err); + updateStatus('Error on sync!'); + $scope.$apply(); return; } @@ -130,7 +136,9 @@ define(function(require) { function listLocalMessages(options, callback) { emailDao.listMessages(options, function(err, emails) { if (err) { - console.error(err); + console.log(err); + updateStatus('Error listing cache!'); + $scope.$apply(); return; } diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index d74e332..1788b07 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -337,6 +337,11 @@ define(function(require) { // decrypt items decryptList(encryptedList, function(err, decryptedList) { + if (err) { + callback(err); + return; + } + // replace encrypted subject and body for (var j = 0; j < displayList.length; j++) { displayList[j].subject = decryptedList[j].subject; @@ -420,7 +425,7 @@ define(function(require) { return; } - fetchList(options, function(emails) { + fetchList(function(emails) { // delete old items from db self._devicestorage.removeList(dbType, function(err) { if (err) { @@ -434,7 +439,7 @@ define(function(require) { }); }); - function fetchList(folder, callback) { + function fetchList(callback) { var encryptedList = []; // fetch imap folder's message list @@ -456,13 +461,13 @@ define(function(require) { }); // fetch message bodies - fetchBodies(encryptedList, folder, function(messages) { + fetchBodies(encryptedList, function(messages) { callback(messages); }); }); } - function fetchBodies(messageList, folder, callback) { + function fetchBodies(messageList, callback) { var emails = []; if (messageList.length < 1) { @@ -476,7 +481,7 @@ define(function(require) { _.each(messageList, function(messageItem) { self.imapGetMessage({ - folder: folder, + folder: options.folder, uid: messageItem.uid }, function(err, message) { if (err) { @@ -526,10 +531,7 @@ define(function(require) { * @param {String} options.messageId The */ EmailDAO.prototype.imapGetMessage = function(options, callback) { - var self = this, - expectedItems, - itemCounter = 0, - message /*, attachments = []*/ ; + var self = this; // validate options if (!options.folder || !options.uid) { @@ -540,42 +542,18 @@ define(function(require) { } function messageReady(err, gottenMessage) { - message = gottenMessage; - itemCounter++; - // remember how many items should be fetched before the callback fires - expectedItems = (message.attachments instanceof Array) ? message.attachments.length + 1 : 1; - - // TODO: remove once attachments work again - if (itemCounter > 1) { + if (err || !gottenMessage) { + callback({ + errMsg: 'Error fetching message body!', + err: err + }); return; } // return message - callback(null, message); - - //check(); + callback(null, gottenMessage); } - // function attachmentReady(err, gottenAttachment) { - // attachments.push(gottenAttachment); - // itemCounter++; - // check(); - // } - - // function check() { - // // go for another round you don't yet know how mich to fetch or you haven't fetch enough - // if (!expectedItems || itemCounter < expectedItems) { - // return; - // } - - // // overwrite attachments array with the uint8array variant - // message.attachments = (attachments.length > 0) ? attachments : undefined; - // // cache message object in memory - // self.cacheItem(options.folder, message); - - // callback(null, message); - // } - self._imapClient.getMessage({ path: options.folder, uid: options.uid, diff --git a/test/integration/email-dao-test.js b/test/integration/email-dao-test.js index 93c155c..5aca69d 100644 --- a/test/integration/email-dao-test.js +++ b/test/integration/email-dao-test.js @@ -35,7 +35,7 @@ define(function(require) { }); }); - describe('IMAP sync messages', function() { + describe('IMAP sync INBOX messages', function() { it('should work', function(done) { emailDao.imapSync({ folder: 'INBOX', @@ -48,7 +48,7 @@ define(function(require) { }); }); - describe('IMAP sync messages', function() { + describe('IMAP list INBOX messages', function() { it('should work', function(done) { emailDao.listMessages({ folder: 'INBOX', @@ -63,5 +63,33 @@ define(function(require) { }); }); + describe('IMAP sync SENT messages', function() { + it('should work', function(done) { + emailDao.imapSync({ + folder: '[Gmail]/Gesendet', + offset: -num, + num: offset + }, function(err) { + expect(err).to.not.exist; + done(); + }); + }); + }); + + describe('IMAP list SENT messages', function() { + it('should work', function(done) { + emailDao.listMessages({ + folder: '[Gmail]/Gesendet', + offset: offset, + num: num + }, function(err, emails) { + expect(err).to.not.exist; + expect(emails).to.exist; + expect(emails.length).to.be.at.least(0); + done(); + }); + }); + }); + }); }); \ No newline at end of file