Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…or instead of server error, Issue #553
  • Loading branch information...
commit 4bd8c72f5ed2823178afc7b3ea5df9a3bd40928a 1 parent 01d73e3
Christian Amor Kvalheim authored March 30, 2012
4  lib/mongodb/connection/connection.js
@@ -37,7 +37,7 @@ var Connection = exports.Connection = function(id, socketOptions) {
37 37
 }
38 38
 
39 39
 // Set max bson size
40  
-Connection.DEFAULT_MAX_BSON_SIZE = 16777216;
  40
+Connection.DEFAULT_MAX_BSON_SIZE = 1024 * 1024 * 4;
41 41
 
42 42
 // Inherit event emitter so we can emit stuff wohoo
43 43
 inherits(Connection, EventEmitter);
@@ -122,11 +122,13 @@ Connection.prototype.write = function(command, callback) {
122 122
     if(Array.isArray(command)) {
123 123
       for(var i = 0; i < command.length; i++) {
124 124
         var binaryCommand = command[i].toBinary()
  125
+        if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
125 126
         if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
126 127
         var r = this.writeSteam.write(binaryCommand);
127 128
       }
128 129
     } else {
129 130
       var binaryCommand = command.toBinary()
  131
+      if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
130 132
       if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
131 133
       var r = this.writeSteam.write(binaryCommand);
132 134
     }    
15  test/insert_test.js
@@ -1137,6 +1137,21 @@ exports.shouldCorrectlyInsertDocWithCustomId = function(test) {
1137 1137
 }
1138 1138
 
1139 1139
 /**
  1140
+ * @ignore
  1141
+ */
  1142
+exports.shouldFailDueToInsertBeingBiggerThanMaxDocumentSizeAllowed = function(test) {
  1143
+  var binary = new Binary(new Buffer(client.serverConfig.checkoutWriter().maxBsonSize + 100));
  1144
+  // Create a collection
  1145
+  client.createCollection('shouldFailDueToInsertBeingBiggerThanMaxDocumentSizeAllowed', function(err, collection) {
  1146
+    collection.insert({doc:binary}, {safe:true}, function(err, result) {      
  1147
+      test.ok(err != null);
  1148
+      test.equal(null, result);
  1149
+      test.done();
  1150
+    });
  1151
+  });  
  1152
+}
  1153
+
  1154
+/**
1140 1155
  * Retrieve the server information for the current
1141 1156
  * instance of the db client
1142 1157
  * 

0 notes on commit 4bd8c72

Please sign in to comment.
Something went wrong with that request. Please try again.