Last active
August 29, 2015 14:08
-
-
Save tcrosen/41618192eb251ec25276 to your computer and use it in GitHub Desktop.
Don't ignore Node error parameters. Silent errors cause lots of lost time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I was performing a simple update of a MongoDB object | |
// .update() doesn't return the data after execution | |
// Because I wasn't concerned with the return value from .update(), | |
// I foolishly forgot to add callback parameters to account for a possible error being returned. | |
// The result was a silent failure that was breaking my tests causing much frustration. | |
// bad | |
function savePerson(newPersonData, done) { | |
Person.findByEmail({ email: newPersonData.email ), function(err, person) { | |
if (err) { done(err); } | |
_.merge(person, newPersonData); | |
// silently fails because update() does not work for objects with an _id | |
person.update(function() { | |
done(null, person); | |
}); | |
} | |
} | |
// good | |
function savePerson(newPersonData, done) { | |
Person.findByEmail({ email: newPersonData.email ), function(err, person) { | |
if (err) { done(err); } | |
_.merge(person, newPersonData); | |
// correctly passes err to parent function | |
person.update(function(err) { | |
if (err) { done(err); } | |
done(null, person); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment