Permalink
Browse files

Check whether there's a callback to handle the open error

  • Loading branch information...
1 parent 851281c commit 230908fe03980f32880f535ed6b0613a854742ba @guileen committed Mar 20, 2012
Showing with 17 additions and 10 deletions.
  1. +3 −2 lib/mongoskin/admin.js
  2. +2 −6 lib/mongoskin/collection.js
  3. +2 −1 lib/mongoskin/cursor.js
  4. +2 −1 lib/mongoskin/db.js
  5. +8 −0 lib/mongoskin/utils.js
View
@@ -1,4 +1,5 @@
-var Admin = require('mongodb').Admin;
+var Admin = require('mongodb').Admin
+ , utils = require('./utils');
var SkinAdmin = exports.SkinAdmin = function(skinDb) {
this.skinDb = skinDb;
@@ -10,7 +11,7 @@ var bindSkin = function(name, method) {
var args = arguments.length > 0 ? Array.prototype.slice.call(arguments, 0) : [];
return this.skinDb.open(function(err, db) {
if (err) {
- args[args.length - 1](err);
+ utils.error(err, args, 'SkinAdmin.' + name);
} else {
method.apply(this.nativeAdmin, args);
}
@@ -29,6 +29,7 @@ var __slice = Array.prototype.slice,
events = require('events'),
Collection = require('mongodb').Collection,
SkinCursor = require('./cursor').SkinCursor,
+ utils = require('./utils'),
STATE_CLOSE = 0,
STATE_OPENNING = 1,
STATE_OPEN = 2;
@@ -69,12 +70,7 @@ var bindSkin = function(name, method) {
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
this.open(function(err, collection) {
if (err) {
- var cb = args.pop();
- if(cb) {
- cb(err)
- } else {
- console.error("Error occured with no callback to handle it while calling SkinCollection." + name, err);
- }
+ utils.error(err, args, 'SkinCollection.' + name);
} else {
method.apply(collection, args);
}
View
@@ -1,6 +1,7 @@
var __slice = Array.prototype.slice,
events = require('events'),
Cursor = require('mongodb').Cursor,
+ utils = require('./utils'),
STATE_CLOSE = 0,
STATE_OPENNING = 1,
STATE_OPEN = 2;
@@ -54,7 +55,7 @@ var bindSkin = function(name, method) {
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
this.open(function(err, cursor) {
if (err) {
- args[args.length - 1](err);
+ utils.error(err, args, 'SkinCursor.' + name);
} else {
method.apply(cursor, args);
}
View
@@ -1,6 +1,7 @@
var __slice = Array.prototype.slice,
mongodb = require('mongodb'),
events = require('events'),
+ utils = require('./utils'),
SkinAdmin = require('./admin').SkinAdmin,
SkinCollection = require('./collection').SkinCollection,
SkinGridStore = require('./gridfs').SkinGridStore,
@@ -185,7 +186,7 @@ var bindSkin = function(name, method) {
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
return this.open(function(err, db) {
if (err) {
- return args[args.length - 1](err);
+ utils.error(err, args, 'SkinDb.' + name);
} else {
return method.apply(db, args);
}
View
@@ -0,0 +1,8 @@
+exports.error = function(err, args, name) {
+ var cb = args.pop();
+ if(cb && typeof cb === 'function') {
+ cb(err)
+ } else {
+ console.error("Error occured with no callback to handle it while calling " + name, err);
+ }
+}

0 comments on commit 230908f

Please sign in to comment.