1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 02:42:17 -05:00

cleanup error handling in email dao and mail-list controller

This commit is contained in:
Tankred Hase 2013-10-04 13:15:16 +02:00
parent 567a2d19ed
commit 6e6012bd78
3 changed files with 59 additions and 45 deletions

View File

@ -98,7 +98,9 @@ define(function(require) {
emailDao.imapLogin(function(err) { emailDao.imapLogin(function(err) {
if (err) { if (err) {
console.error(err); console.log(err);
updateStatus('Error on login!');
$scope.$apply();
return; return;
} }
@ -109,7 +111,9 @@ define(function(require) {
function syncImapFolder(options, callback) { function syncImapFolder(options, callback) {
emailDao.unreadMessages(getFolder().path, function(err, unreadCount) { emailDao.unreadMessages(getFolder().path, function(err, unreadCount) {
if (err) { if (err) {
console.error(err); console.log(err);
updateStatus('Error on sync!');
$scope.$apply();
return; return;
} }
// set unread count in folder model // set unread count in folder model
@ -118,7 +122,9 @@ define(function(require) {
emailDao.imapSync(options, function(err) { emailDao.imapSync(options, function(err) {
if (err) { if (err) {
console.error(err); console.log(err);
updateStatus('Error on sync!');
$scope.$apply();
return; return;
} }
@ -130,7 +136,9 @@ define(function(require) {
function listLocalMessages(options, callback) { function listLocalMessages(options, callback) {
emailDao.listMessages(options, function(err, emails) { emailDao.listMessages(options, function(err, emails) {
if (err) { if (err) {
console.error(err); console.log(err);
updateStatus('Error listing cache!');
$scope.$apply();
return; return;
} }

View File

@ -337,6 +337,11 @@ define(function(require) {
// decrypt items // decrypt items
decryptList(encryptedList, function(err, decryptedList) { decryptList(encryptedList, function(err, decryptedList) {
if (err) {
callback(err);
return;
}
// replace encrypted subject and body // replace encrypted subject and body
for (var j = 0; j < displayList.length; j++) { for (var j = 0; j < displayList.length; j++) {
displayList[j].subject = decryptedList[j].subject; displayList[j].subject = decryptedList[j].subject;
@ -420,7 +425,7 @@ define(function(require) {
return; return;
} }
fetchList(options, function(emails) { fetchList(function(emails) {
// delete old items from db // delete old items from db
self._devicestorage.removeList(dbType, function(err) { self._devicestorage.removeList(dbType, function(err) {
if (err) { if (err) {
@ -434,7 +439,7 @@ define(function(require) {
}); });
}); });
function fetchList(folder, callback) { function fetchList(callback) {
var encryptedList = []; var encryptedList = [];
// fetch imap folder's message list // fetch imap folder's message list
@ -456,13 +461,13 @@ define(function(require) {
}); });
// fetch message bodies // fetch message bodies
fetchBodies(encryptedList, folder, function(messages) { fetchBodies(encryptedList, function(messages) {
callback(messages); callback(messages);
}); });
}); });
} }
function fetchBodies(messageList, folder, callback) { function fetchBodies(messageList, callback) {
var emails = []; var emails = [];
if (messageList.length < 1) { if (messageList.length < 1) {
@ -476,7 +481,7 @@ define(function(require) {
_.each(messageList, function(messageItem) { _.each(messageList, function(messageItem) {
self.imapGetMessage({ self.imapGetMessage({
folder: folder, folder: options.folder,
uid: messageItem.uid uid: messageItem.uid
}, function(err, message) { }, function(err, message) {
if (err) { if (err) {
@ -526,10 +531,7 @@ define(function(require) {
* @param {String} options.messageId The * @param {String} options.messageId The
*/ */
EmailDAO.prototype.imapGetMessage = function(options, callback) { EmailDAO.prototype.imapGetMessage = function(options, callback) {
var self = this, var self = this;
expectedItems,
itemCounter = 0,
message /*, attachments = []*/ ;
// validate options // validate options
if (!options.folder || !options.uid) { if (!options.folder || !options.uid) {
@ -540,42 +542,18 @@ define(function(require) {
} }
function messageReady(err, gottenMessage) { function messageReady(err, gottenMessage) {
message = gottenMessage; if (err || !gottenMessage) {
itemCounter++; callback({
// remember how many items should be fetched before the callback fires errMsg: 'Error fetching message body!',
expectedItems = (message.attachments instanceof Array) ? message.attachments.length + 1 : 1; err: err
});
// TODO: remove once attachments work again
if (itemCounter > 1) {
return; return;
} }
// return message // return message
callback(null, message); callback(null, gottenMessage);
//check();
} }
// 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({ self._imapClient.getMessage({
path: options.folder, path: options.folder,
uid: options.uid, uid: options.uid,

View File

@ -35,7 +35,7 @@ define(function(require) {
}); });
}); });
describe('IMAP sync messages', function() { describe('IMAP sync INBOX messages', function() {
it('should work', function(done) { it('should work', function(done) {
emailDao.imapSync({ emailDao.imapSync({
folder: 'INBOX', 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) { it('should work', function(done) {
emailDao.listMessages({ emailDao.listMessages({
folder: 'INBOX', 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();
});
});
});
}); });
}); });