Skip to content

Instantly share code, notes, and snippets.

@tcrosen
Last active August 29, 2015 14:08
Show Gist options
  • Save tcrosen/41618192eb251ec25276 to your computer and use it in GitHub Desktop.
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.
// 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