Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-1081

CommandResult.getException not detecting duplicate key errors reported in errObjects field

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.12.0, 3.0.0
    • Affects Version/s: 2.11.3
    • Labels:
      None
    • Environment:
      Windows Server 2008 R2 Enterprise, 64-bit, 144GB RAM,

      The getException() method of the com.mongodb.CommandResult class does not cover all the cases in which a duplicate key error occurs.

      The main reason for this behavior is that it is assumed that the error code is contained as the value of the "code" key in the root JSON response object. Here's a response object that I get when issuing an "insert" command:

      { 
      	"serverUsed" : "ushesb0wm30srv/165.226.120.63:27017" , 
      	"shards" : [ "127.0.0.1:27019" , "127.0.0.1:27020"] , 
      	"shardRawGLE" : { 
      		"127.0.0.1:27019" : { 
      			"err" : "E11000 duplicate key error index: ig_hotcell.messages.$uniqueMessage  dup key: { : \"1003 10275008\", : 1333290898000, : 2, : \"TEMPERATURE\", : \"NORMAL\", : \"\" }" , 
      			"code" : 11000 , 
      			"n" : 0 , 
      			"connectionId" : 4894 , 
      			"ok" : 1.0
      		} , 
      		"127.0.0.1:27020" : { 
      			"err" : "E11000 duplicate key error index: ig_hotcell.messages.$uniqueMessage  dup key: { : \"1003 10275008\", : 1333064214000, : 2, : \"TEMPERATURE\", : \"NORMAL\", : \"\" }" , 
      			"code" : 11000 , 
      			"n" : 0 , 
      			"connectionId" : 934 , 
      			"ok" : 1.0
      		}
      	} , 
      	"n" : 0 , 
      	"err" : "E11000 duplicate key error index: ig_hotcell.messages.$uniqueMessage  dup key: { : \"1003 10275008\", : 1333290898000, : 2, : \"TEMPERATURE\", : \"NORMAL\", : \"\" }" , 
      	"errs" : [ "E11000 duplicate key error index: ig_hotcell.messages.$uniqueMessage  dup key: { : \"1003 10275008\", : 1333290898000, : 2, : \"TEMPERATURE\", : \"NORMAL\", : \"\" }" , 
      				"E11000 duplicate key error index: ig_hotcell.messages.$uniqueMessage  dup key: { : \"1003 10275008\", : 1333064214000, : 2, : \"TEMPERATURE\", : \"NORMAL\", : \"\" }"] , 
      	"errObjects" : [ { 
      		"err" : "E11000 duplicate key error index: ig_hotcell.messages.$uniqueMessage  dup key: { : \"1003 10275008\", : 1333290898000, : 2, : \"TEMPERATURE\", : \"NORMAL\", : \"\" }" , 
      		"code" : 11000 , 
      		"n" : 0 , 
      		"connectionId" : 4894 , 
      		"ok" : 1.0
      	} , 
      	{ 
      		"err" : "E11000 duplicate key error index: ig_hotcell.messages.$uniqueMessage  dup key: { : \"1003 10275008\", : 1333064214000, : 2, : \"TEMPERATURE\", : \"NORMAL\", : \"\" }" , 
      		"code" : 11000 , 
      		"n" : 0 , 
      		"connectionId" : 934 , 
      		"ok" : 1.0
      	}] , 
      	"ok" : 1.0
      }
      

      There is no "code" attribute in the root JSON object, therefore a WriteConcernException is thrown, instead of a more specialized MongoException.DuplicateKey one. Definitely, it is the case of a duplicate key error, but the getException (or the getError) method does not implement the best efforts to identify a duplicate key error.

      The changed structure of the response object (compared to the expected one) might be because of the fact that the original "insert" command was issued to a mongos process, not to a mongod process.

      Such an issue limits the driver API clients in finding the duplicate key errors with 100% accuracy, which leads to MongoException.DuplicateKey exception not being 100% leveraged in client code.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            andreinic Andrei Nicusan
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: