1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 08:52:15 -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) {
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;
}

View File

@ -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,

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) {
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();
});
});
});
});
});