[JAVA-1352] DuplicateKeyException should provide the propertyname which is duplicated Created: 06/Aug/14  Updated: 12/Aug/14  Resolved: 12/Aug/14

Status: Closed
Project: Java Driver
Component/s: API, Error Handling, Write Operations
Affects Version/s: 2.12.2
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Dominic Sachs [X] Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 8


Issue Links:
Related
related to SERVER-14801 duplicate key error should report fie... Closed
Backwards Compatibility: Fully Compatible

 Description   

It would be nice if a DuplicateKeyException is thrown and the duplicated property is returned by the exception object.



 Comments   
Comment by Jeffrey Yemin [ 12/Aug/14 ]

I opened SERVER-14801 and linked it to this ticket, as there's nothing we can do in the driver itself. Feel free to comment on or vote up that ticket.

Regards,
Jeff

Comment by Jeffrey Yemin [ 06/Aug/14 ]

Also, note that in general this could be the result of a unique key violation of a compound index, so there may be more than one property name, e.g.:

db.test.ensureIndex( { a: 1, b : 1 }, { unique: true, name : 'index_1' }

A duplicate key error on that index reports this errmsg:

"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: admin.test.$index_1  dup key: { : 1.0, : 2.0 }"

Comment by Ross Lawley [ 06/Aug/14 ]

Currently, that information is not returned by the server - just the string error message containing the index name and the duplicated key. I think the best course of action would be to add a SERVER ticket to get this information included.

Regards,

Ross

Comment by Dominic Sachs [X] [ 06/Aug/14 ]

Hi Ross,

thanks. I have to parse the String, to get the property name? I'm looking for the raw property name.

Comment by Ross Lawley [ 06/Aug/14 ]

Hi Noodles,

You can get the error message back from the server which describes the duplicated key by looking in the commandResult of the insert.

For example:

DBObject obj = new BasicDBObject();
try {
    collection.insert(obj, WriteConcern.SAFE);
    collection.insert(obj, WriteConcern.SAFE);
} catch (DuplicateKeyException e) {
    e.getCommandResult().getString("err");  // Contains information about the duplication error
}

Generated at Thu Feb 08 08:54:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.