-
Type: Improvement
-
Resolution: Done
-
Priority: Minor - P4
-
Affects Version/s: 2.11.3
-
Component/s: Command Operations, Error Handling
-
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.