Bug 1056918 - Allow distinguishing between 'not found' and 'database error' programmatically r=standard8

This commit is contained in:
Adam Roach [:abr] 2014-08-26 09:56:42 -05:00
Родитель 94631455e0
Коммит 933182ec4c
2 изменённых файлов: 33 добавлений и 17 удалений

Просмотреть файл

@ -489,6 +489,9 @@ let LoopContactsInternal = Object.freeze({
* finished. The first argument passed will be an * finished. The first argument passed will be an
* `Error` object or `null`. The second argument will * `Error` object or `null`. The second argument will
* be the contact object, if successful. * be the contact object, if successful.
* If no object matching guid could be found,
* then the callback is called with both arguments
* set to `null`.
*/ */
get: function(guid, callback) { get: function(guid, callback) {
LoopStorage.getStore(kObjectStoreName, (err, store) => { LoopStorage.getStore(kObjectStoreName, (err, store) => {
@ -507,8 +510,7 @@ let LoopContactsInternal = Object.freeze({
request.onsuccess = event => { request.onsuccess = event => {
if (!event.target.result) { if (!event.target.result) {
callback(new Error("Contact with " + kKeyPath + " '" + callback(null, null);
guid + "' could not be found"));;
return; return;
} }
let contact = extend({}, event.target.result); let contact = extend({}, event.target.result);
@ -528,6 +530,9 @@ let LoopContactsInternal = Object.freeze({
* finished. The first argument passed will be an * finished. The first argument passed will be an
* `Error` object or `null`. The second argument will * `Error` object or `null`. The second argument will
* be the contact object, if successfull. * be the contact object, if successfull.
* If no object matching serviceId could be found,
* then the callback is called with both arguments
* set to `null`.
*/ */
getByServiceId: function(serviceId, callback) { getByServiceId: function(serviceId, callback) {
LoopStorage.getStore(kObjectStoreName, (err, store) => { LoopStorage.getStore(kObjectStoreName, (err, store) => {
@ -547,8 +552,7 @@ let LoopContactsInternal = Object.freeze({
request.onsuccess = event => { request.onsuccess = event => {
if (!event.target.result) { if (!event.target.result) {
callback(new Error("Contact with " + kServiceIdIndex + " '" + callback(null, null);
serviceId + "' could not be found"));
return; return;
} }
@ -658,6 +662,11 @@ let LoopContactsInternal = Object.freeze({
return; return;
} }
if (!contact) {
callback(new Error("Contact with " + kKeyPath + " '" +
guid + "' could not be found"));
}
LoopStorage.getStore(kObjectStoreName, (err, store) => { LoopStorage.getStore(kObjectStoreName, (err, store) => {
if (err) { if (err) {
callback(err); callback(err);
@ -702,6 +711,11 @@ let LoopContactsInternal = Object.freeze({
return; return;
} }
if (!contact) {
callback(new Error("Contact with " + kKeyPath + " '" +
guid + "' could not be found"));
}
contact.blocked = true; contact.blocked = true;
this.update(contact, callback); this.update(contact, callback);
}); });
@ -723,6 +737,11 @@ let LoopContactsInternal = Object.freeze({
return; return;
} }
if (!contact) {
callback(new Error("Contact with " + kKeyPath + " '" +
guid + "' could not be found"));
}
contact.blocked = false; contact.blocked = false;
this.update(contact, callback); this.update(contact, callback);
}); });

Просмотреть файл

@ -230,10 +230,9 @@ add_task(function* () {
// Get a non-existent contact. // Get a non-existent contact.
deferred = Promise.defer(); deferred = Promise.defer();
LoopContacts.get(1000, err => { LoopContacts.get(1000, (err, contact) => {
Assert.ok(err, "There should be an error"); Assert.ok(!err, "There shouldn't be an error");
Assert.equal(err.message, "Contact with _guid '1000' could not be found", Assert.ok(!contact, "There shouldn't be a contact");
"Error message should be correct");
deferred.resolve(); deferred.resolve();
}); });
yield deferred.promise; yield deferred.promise;
@ -250,10 +249,9 @@ add_task(function* () {
LoopContacts.remove(toRemove, err => { LoopContacts.remove(toRemove, err => {
Assert.ok(!err, "There shouldn't be an error"); Assert.ok(!err, "There shouldn't be an error");
LoopContacts.get(toRemove, err => { LoopContacts.get(toRemove, (err, contact) => {
Assert.ok(err, "There should be an error"); Assert.ok(!err, "There shouldn't be an error");
Assert.equal(err.message, "Contact with _guid '" + toRemove + "' could not be found", Assert.ok(!contact, "There shouldn't be a contact");
"Error message should be correct");
deferred.resolve(); deferred.resolve();
}); });
}); });
@ -261,10 +259,9 @@ add_task(function* () {
// Remove a non-existing contact. // Remove a non-existing contact.
deferred = Promise.defer(); deferred = Promise.defer();
LoopContacts.remove(1000, err => { LoopContacts.remove(1000, (err, contact) => {
Assert.ok(err, "There should be an error"); Assert.ok(!err, "There shouldn't be an error");
Assert.equal(err.message, "Contact with _guid '1000' could not be found", Assert.ok(!contact, "There shouldn't be a contact");
"Error message should be correct");
deferred.resolve(); deferred.resolve();
}); });
yield deferred.promise; yield deferred.promise;
@ -323,7 +320,7 @@ add_task(function* () {
_guid: 1000, _guid: 1000,
bday: newBday bday: newBday
}; };
LoopContacts.update(toUpdate, err => { LoopContacts.update(toUpdate, (err, contact) => {
Assert.ok(err, "There should be an error"); Assert.ok(err, "There should be an error");
Assert.equal(err.message, "Contact with _guid '1000' could not be found", Assert.equal(err.message, "Contact with _guid '1000' could not be found",
"Error message should be correct"); "Error message should be correct");