admin管理员组文章数量:1415484
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 badges1 Answer
Reset to default 2I 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);
};
};
本文标签: javascriptNodejs proper callback error handleStack Overflow
版权声明:本文标题:javascript - Node.js proper callback error handle - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745232348a2648880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论