[JAVA-2598]  com.mongodb.MongoWriteException instead of com.mongodb.DuplicateKeyException Created: 14/Sep/17  Updated: 11/Oct/18  Resolved: 11/Oct/18

Status: Closed
Project: Java Driver
Component/s: Write Operations
Affects Version/s: 3.5.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: kant kodali Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related

 Description   


returns com.mongodb.MongoWriteException: E11000 duplicate key error index during upserts instead of com.mongodb.DuplicateKeyException. It would be helpful if the driver returns com.mongodb.DuplicateKeyException so a user can take a useful action.



 Comments   
Comment by Jeffrey Yemin [ 11/Oct/18 ]

Changing the exception would be a breaking change for some applications, so we won't make the change you requested here. But your application can determine if a MongoWriteException is a duplicate key error with code like this:

        try {
            collection.insertOne(document);
        } catch (MongoWriteException e) {
            if (e.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
                // do something
            }
        }

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