Permalink
Browse files

Check bson size of documents against maxBsonSize and throw client err…

…or instead of server error, Issue #553
  • Loading branch information...
1 parent 01d73e3 commit 4bd8c72f5ed2823178afc7b3ea5df9a3bd40928a @christkv committed Mar 30, 2012
Showing with 18 additions and 1 deletion.
  1. +3 −1 lib/mongodb/connection/connection.js
  2. +15 −0 test/insert_test.js
@@ -37,7 +37,7 @@ var Connection = exports.Connection = function(id, socketOptions) {
}
// Set max bson size
-Connection.DEFAULT_MAX_BSON_SIZE = 16777216;
+Connection.DEFAULT_MAX_BSON_SIZE = 1024 * 1024 * 4;
// Inherit event emitter so we can emit stuff wohoo
inherits(Connection, EventEmitter);
@@ -122,11 +122,13 @@ Connection.prototype.write = function(command, callback) {
if(Array.isArray(command)) {
for(var i = 0; i < command.length; i++) {
var binaryCommand = command[i].toBinary()
+ if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
var r = this.writeSteam.write(binaryCommand);
}
} else {
var binaryCommand = command.toBinary()
+ if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
var r = this.writeSteam.write(binaryCommand);
}
View
@@ -1136,6 +1136,21 @@ exports.shouldCorrectlyInsertDocWithCustomId = function(test) {
});
}
+/**
+ * @ignore
+ */
+exports.shouldFailDueToInsertBeingBiggerThanMaxDocumentSizeAllowed = function(test) {
+ var binary = new Binary(new Buffer(client.serverConfig.checkoutWriter().maxBsonSize + 100));
+ // Create a collection
+ client.createCollection('shouldFailDueToInsertBeingBiggerThanMaxDocumentSizeAllowed', function(err, collection) {
+ collection.insert({doc:binary}, {safe:true}, function(err, result) {
+ test.ok(err != null);
+ test.equal(null, result);
+ test.done();
+ });
+ });
+}
+
/**
* Retrieve the server information for the current
* instance of the db client

0 comments on commit 4bd8c72

Please sign in to comment.