DRY error handling?

40 views
Skip to first unread message

Michael Holroyd

unread,
May 6, 2013, 6:25:33 PM5/6/13
to node-mong...@googlegroups.com
Does anyone have a slick solution for including a default error handling function with each call to mongo? I hate wasting many lines of code with if(err) {...} statements in each query.

My stab at it:

var logIfDbError = function(e)
{
  if(e){
    console.log(e); // and send to analytics
  }
};

db.collection('myCollection', function(err, myCollection) { logIfDbError(err);
  myCollection.insert(newDocument, logIfDbError);
  myCollection.count(function(err,count){ logIfDbError(err);
    console.log(count); // and do work
  });
});

Jonathan Ong

unread,
May 6, 2013, 10:54:54 PM5/6/13
to node-mong...@googlegroups.com
welcome to node.js :x

if you really hate it, you can try promises or something, but personally i just learnt to deal with it.
Reply all
Reply to author
Forward
0 new messages