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

cleanup error handling in email dao

This commit is contained in:
Tankred Hase 2013-10-04 15:47:30 +02:00
parent 6e6012bd78
commit f045a71ebe
2 changed files with 25 additions and 19 deletions

View File

@ -320,14 +320,10 @@ define(function(require) {
callback(err); callback(err);
return; return;
} }
if (emails.length === 0) {
callback(null, []);
return;
}
// find encrypted items // find encrypted items
emails.forEach(function(i) { emails.forEach(function(i) {
if (i.body.indexOf(str.cryptPrefix) !== -1 && i.body.indexOf(str.cryptSuffix) !== -1) { if (typeof i.body === 'string' && i.body.indexOf(str.cryptPrefix) !== -1 && i.body.indexOf(str.cryptSuffix) !== -1) {
// add item to plaintext list for display later // add item to plaintext list for display later
displayList.push(i); displayList.push(i);
// parse ct object from ascii armored message block // parse ct object from ascii armored message block
@ -335,6 +331,11 @@ define(function(require) {
} }
}); });
if (encryptedList.length === 0) {
callback(null, []);
return;
}
// decrypt items // decrypt items
decryptList(encryptedList, function(err, decryptedList) { decryptList(encryptedList, function(err, decryptedList) {
if (err) { if (err) {
@ -425,17 +426,21 @@ define(function(require) {
return; return;
} }
fetchList(function(emails) { fetchList(function(err, emails) {
if (err) {
callback(err);
return;
}
// 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) {
callback(err); callback(err);
return; return;
} }
// persist encrypted list in device storage // persist encrypted list in device storage
self._devicestorage.storeList(emails, dbType, function(err) { self._devicestorage.storeList(emails, dbType, callback);
callback(err);
});
}); });
}); });
@ -449,7 +454,7 @@ define(function(require) {
num: options.num num: options.num
}, function(err, emails) { }, function(err, emails) {
if (err) { if (err) {
console.log(err); callback(err);
return; return;
} }
@ -461,9 +466,7 @@ define(function(require) {
}); });
// fetch message bodies // fetch message bodies
fetchBodies(encryptedList, function(messages) { fetchBodies(encryptedList, callback);
callback(messages);
});
}); });
} }
@ -471,12 +474,12 @@ define(function(require) {
var emails = []; var emails = [];
if (messageList.length < 1) { if (messageList.length < 1) {
callback(emails); callback(null, emails);
return; return;
} }
var after = _.after(messageList.length, function() { var after = _.after(messageList.length, function() {
callback(emails); callback(null, emails);
}); });
_.each(messageList, function(messageItem) { _.each(messageList, function(messageItem) {
@ -485,7 +488,7 @@ define(function(require) {
uid: messageItem.uid uid: messageItem.uid
}, function(err, message) { }, function(err, message) {
if (err) { if (err) {
console.log(err); callback(err);
return; return;
} }

View File

@ -426,14 +426,17 @@ define(function(require) {
it('should work', function(done) { it('should work', function(done) {
devicestorageStub.listItems.yields(null, [{ devicestorageStub.listItems.yields(null, [{
body: '' body: app.string.cryptPrefix + btoa(JSON.stringify({})) + app.string.cryptSuffix
}]); }]);
keychainStub.getPublicKeys.yields(null, [{ keychainStub.getPublicKeys.yields(null, [{
_id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3", _id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3",
userId: "safewithme.testuser@gmail.com", userId: "safewithme.testuser@gmail.com",
publicKey: publicKey publicKey: publicKey
}]); }]);
cryptoStub.decryptListForUser.yields(null, []); cryptoStub.decryptListForUser.yields(null, [{
body: 'test body',
subject: 'test subject'
}]);
emailDao.listMessages({ emailDao.listMessages({
folder: 'INBOX', folder: 'INBOX',
@ -444,7 +447,7 @@ define(function(require) {
expect(keychainStub.getPublicKeys.calledOnce).to.be.true; expect(keychainStub.getPublicKeys.calledOnce).to.be.true;
expect(cryptoStub.decryptListForUser.calledOnce).to.be.true; expect(cryptoStub.decryptListForUser.calledOnce).to.be.true;
expect(err).to.not.exist; expect(err).to.not.exist;
expect(emails.length).to.equal(0); expect(emails.length).to.equal(1);
done(); done();
}); });
}); });