javascript - Node.js proper callback error handle - Stack Overflow

Lets take a look at the following code:mongodb.jsvar mongo = require('mongodb'), mongodb = mo

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?

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?

Share Improve this question asked Aug 23, 2012 at 13:45 onlineracoononlineracoon 2,9705 gold badges49 silver badges66 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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);
  };
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745232348a4617734.html

相关推荐

  • javascript - Node.js proper callback error handle - Stack Overflow

    Lets take a look at the following code:mongodb.jsvar mongo = require('mongodb'), mongodb = mo

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信