1

Lets take a look at the following code:

mongodb.js

var mongo = require('mongodb')
  , mongodb = module.exports;

mongodb.database = function(key, dbName, dbServer, callback){
  var db = new mongo.Db(dbName, dbServer);
  db.open(function(error, connection){ // 5
    if(error){ // 1
      throw error;
    }

    return callback(null, connection);
  });
};

mongodb.collection = function(dbKey, collName, callback){
  mongodb.database(dbKey, function(error, connection){
    connection.collection(collName, function(error, collection){ // 2
      if(error){ // 3
        throw error;
      }

      return callback(null, collection);
    });
  });
}

model.js

var db = require('./mongodb.js');
var model = module.exports;

model.test = function(callback){
  db.collection('users', function(error, collection){ // 4
    // do something.
  });
}

My question is, why do we always do "error, resource" when a callback is passed through, because, take a look at 1

We are dealing with the error, so it can't even reach 2*

Same with 3 and 4 they will always be NULL, because the error is handled on a different layer. Now why wouldn't I just do this at 5:

db.open(function(error, connection){
  if(error){ // 1
    return errorHandler(error);
  }

  return callback(collection);
});

the errorHandler function would be a function that deals with major errors, so we don't have to use them at each layer, same story for for example mongodb.findOne, why do we have to deal with errors when we pass in the callback?

1 Answer 1

2

I use the callback(error,result) pattern mostly when the error is not thrown anywhere (or catched). I.e. you dont want to stop your application just because of some missing parameter. Look at the following code:

function somethingAsync (options, callback) {
  if (!options || !options.name) {
    callback('no name specified'); //we dont want to kill the Application
    return;
  };
  doSomeThingInternal(callback);
};

On the consumer side you can print the error to the client from any internal function.

somethingAsync(null, function (err,res)) {
  if(err) {
    console.log('error occured: ' + err);
  };
};
Sign up to request clarification or add additional context in comments.

2 Comments

You can still catch "uncaught" exceptions, so the application won't stop. That is why I mentioned "errorHandler(error)" where "errorHandler" would be a function that deals with errors. I just don't see the point of passing an error when an database error is critical, and not because of parameters or what so ever.
A common pattern that I use pretty much 100% of the time is to have "if(err) return callback(err); at the top of each callback. Then, errors will be only handled in a single location and bubble through to there. It also let's me "inject" additional errors, such as "if(res.length == 0) return callback(new Error('no items'));"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.