Skip to content

Commit

Permalink
Bug 1056918 - Allow distinguishing between 'not found' and 'database …
Browse files Browse the repository at this point in the history
…error' programmatically r=standard8
  • Loading branch information
adamroach committed Aug 26, 2014
1 parent 9463145 commit 933182e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
27 changes: 23 additions & 4 deletions browser/components/loop/LoopContacts.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ let LoopContactsInternal = Object.freeze({
* finished. The first argument passed will be an
* `Error` object or `null`. The second argument will
* 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) {
LoopStorage.getStore(kObjectStoreName, (err, store) => {
Expand All @@ -507,8 +510,7 @@ let LoopContactsInternal = Object.freeze({

request.onsuccess = event => {
if (!event.target.result) {
callback(new Error("Contact with " + kKeyPath + " '" +
guid + "' could not be found"));;
callback(null, null);
return;
}
let contact = extend({}, event.target.result);
Expand All @@ -528,6 +530,9 @@ let LoopContactsInternal = Object.freeze({
* finished. The first argument passed will be an
* `Error` object or `null`. The second argument will
* 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) {
LoopStorage.getStore(kObjectStoreName, (err, store) => {
Expand All @@ -547,8 +552,7 @@ let LoopContactsInternal = Object.freeze({

request.onsuccess = event => {
if (!event.target.result) {
callback(new Error("Contact with " + kServiceIdIndex + " '" +
serviceId + "' could not be found"));
callback(null, null);
return;
}

Expand Down Expand Up @@ -658,6 +662,11 @@ let LoopContactsInternal = Object.freeze({
return;
}

if (!contact) {
callback(new Error("Contact with " + kKeyPath + " '" +
guid + "' could not be found"));
}

LoopStorage.getStore(kObjectStoreName, (err, store) => {
if (err) {
callback(err);
Expand Down Expand Up @@ -702,6 +711,11 @@ let LoopContactsInternal = Object.freeze({
return;
}

if (!contact) {
callback(new Error("Contact with " + kKeyPath + " '" +
guid + "' could not be found"));
}

contact.blocked = true;
this.update(contact, callback);
});
Expand All @@ -723,6 +737,11 @@ let LoopContactsInternal = Object.freeze({
return;
}

if (!contact) {
callback(new Error("Contact with " + kKeyPath + " '" +
guid + "' could not be found"));
}

contact.blocked = false;
this.update(contact, callback);
});
Expand Down
23 changes: 10 additions & 13 deletions browser/components/loop/test/mochitest/browser_LoopContacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,9 @@ add_task(function* () {

// Get a non-existent contact.
deferred = Promise.defer();
LoopContacts.get(1000, err => {
Assert.ok(err, "There should be an error");
Assert.equal(err.message, "Contact with _guid '1000' could not be found",
"Error message should be correct");
LoopContacts.get(1000, (err, contact) => {
Assert.ok(!err, "There shouldn't be an error");
Assert.ok(!contact, "There shouldn't be a contact");
deferred.resolve();
});
yield deferred.promise;
Expand All @@ -250,21 +249,19 @@ add_task(function* () {
LoopContacts.remove(toRemove, err => {
Assert.ok(!err, "There shouldn't be an error");

LoopContacts.get(toRemove, err => {
Assert.ok(err, "There should be an error");
Assert.equal(err.message, "Contact with _guid '" + toRemove + "' could not be found",
"Error message should be correct");
LoopContacts.get(toRemove, (err, contact) => {
Assert.ok(!err, "There shouldn't be an error");
Assert.ok(!contact, "There shouldn't be a contact");
deferred.resolve();
});
});
yield deferred.promise;

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

0 comments on commit 933182e

Please sign in to comment.