最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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);
  };
};
发布评论

评论列表(0)

  1. 暂无评论