Skip to content

Commit

Permalink
Changes in exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Jul 20, 2011
1 parent 5391007 commit ac698ad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
10 changes: 7 additions & 3 deletions lib/mongodb/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ Admin.prototype.profilingInfo = function(callback) {
var databaseName = this.db.databaseName;
this.db.databaseName = 'admin';

new Cursor(this.db, new Collection(this.db, DbCommand.SYSTEM_PROFILE_COLLECTION), {}).toArray(function(err, items) {
return callback(err, items);
});
try {
new Cursor(this.db, new Collection(this.db, DbCommand.SYSTEM_PROFILE_COLLECTION), {}).toArray(function(err, items) {
return callback(err, items);
});
} catch (err) {
return callback(err, null);
}

self.db.databaseName = databaseName;
};
Expand Down
26 changes: 21 additions & 5 deletions lib/mongodb/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Db.prototype.collectionsInfo = function(collection_name, callback) {
callback(null, new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector));
} else {
return new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector);
}
}
};

/**
Expand Down Expand Up @@ -159,11 +159,19 @@ Db.prototype.collection = function(collectionName, options, callback) {
if(collections.length == 0) {
return callback(new Error("Collection " + collectionName + " does not exist. Currently in strict mode."), null);
} else {
return callback(null, new Collection(self, collectionName, self.pkFactory, options));
try {
return callback(null, new Collection(self, collectionName, self.pkFactory, options));
} catch(err) {
return callback(err, null);

This comment has been minimized.

Copy link
@aheckmann

aheckmann Jul 20, 2011

Contributor

This is a double callback.

}
}
});
} else {
return callback(null, new Collection(self, collectionName, self.pkFactory, options));
try {
return callback(null, new Collection(self, collectionName, self.pkFactory, options));
} catch(err) {
return callback(err, null);

This comment has been minimized.

Copy link
@aheckmann

aheckmann Jul 20, 2011

Contributor

This is a double callback.

}
}
};

Expand Down Expand Up @@ -371,13 +379,21 @@ Db.prototype.createCollection = function(collectionName, options, callback) {
if(found && self.strict) {
callback(new Error("Collection " + collectionName + " already exists. Currently in strict mode."), null); return;
} else if(found){
callback(null, new Collection(self, collectionName, self.pkFactory)); return;
try {
callback(null, new Collection(self, collectionName, self.pkFactory)); return;
} catch(err) {
return callback(err, null);

This comment has been minimized.

Copy link
@aheckmann

aheckmann Jul 20, 2011

Contributor

This is a double callback.

}
}

// Create a new collection and return it
self.executeCommand(DbCommand.createCreateCollectionCommand(self, collectionName, options), {read:false, safe:true}, function(err, result) {
if(err == null && result.documents[0].ok == 1) {
callback(null, new Collection(self, collectionName, self.pkFactory));
try {
callback(null, new Collection(self, collectionName, self.pkFactory));
} catch(err) {
return callback(err, null);

This comment has been minimized.

Copy link
@aheckmann

aheckmann Jul 20, 2011

Contributor

This is a double callback.

}
} else {
err != null ? callback(err, null) : callback(new Error("Error creating collection: " + collectionName), null);
}
Expand Down
54 changes: 21 additions & 33 deletions test/collection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,42 +319,30 @@ var tests = testCase({
},

shouldFailDueToIllegalCollectionNames : function(test) {
try {
client.collection(5, function(err, collection) {});
} catch (err) {
client.collection(5, function(err, collection) {
test.equal("collection name must be a String", err.message);
}

try {
client.collection("", function(err, collection) {});
} catch (err) {
});

client.collection("", function(err, collection) {
test.equal("collection names cannot be empty", err.message);
}

try {
client.collection("te$t", function(err, collection) {});
} catch (err) {
test.equal("collection names must not contain '$'", err.message);
}

try {
client.collection(".test", function(err, collection) {});
} catch (err) {
test.equal("collection names must not start or end with '.'", err.message);
}

try {
client.collection("test.", function(err, collection) {});
} catch (err) {
test.equal("collection names must not start or end with '.'", err.message);
}

try {
client.collection("test..t", function(err, collection) {});
} catch (err) {
});

client.collection("te$t", function(err, collection) {
test.equal("collection names must not contain '$'", err.message);
});

client.collection(".test", function(err, collection) {
test.equal("collection names must not start or end with '.'", err.message);
});

client.collection("test.", function(err, collection) {
test.equal("collection names must not start or end with '.'", err.message);
});

client.collection("test..t", function(err, collection) {
test.equal("collection names cannot be empty", err.message);
test.done();
}
test.done();
});
},

// Test the count result on a collection that does not exist
Expand Down

0 comments on commit ac698ad

Please sign in to comment.