[CSHARP-1236] Non-unique key error handling Created: 08/Apr/15  Updated: 05/Apr/19  Resolved: 08/Apr/15

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Aristarkh Zagorodnikov Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by CSHARP-1390 InsertOneAsync doesn't throw MongoDup... Closed

 Description   

It appears that MongoCollection<TDocument>.InsertOneAsync throws a MongoWriteException which does not have any direct information about the kind of error happened, including non-unique key constraint violation. I checked the source and even single inserts are done via bulk write operations, which are inherently not very good at passing single-request error information. I was expecting MongoDuplicateKeyException to be thrown in case of such violation.
Is this going to change, or should we ignore the existence of MongoDuplicateKeyException and try to interrogate inner exceptions which seems very unwieldy?



 Comments   
Comment by Craig Wilson [ 08/Apr/15 ]

Yes, probably should have been moved. Alas, too late now.

Comment by Aristarkh Zagorodnikov [ 08/Apr/15 ]

Oh, Craig, I'm okay with this (already added an IsDuplicateKeyError extension method for our toolset), it's just the MongoDuplicateKeyException that took me off the right course. Maybe it should be moved to legacy part of the driver?

Comment by Craig Wilson [ 08/Apr/15 ]

I had a reply typed out about that you can check the WriteError.Category field... C#6 will have exception filters which will make this much cleaner.

We opted not to throw a MongoDuplicateKeyException because, well, why is it more special? Instead, we opted for a common consistent way to check for certain types of known errors.

Comment by Aristarkh Zagorodnikov [ 08/Apr/15 ]

P.S. In understand that there is a WriteError field available and we can check the category, it's just the MongoDuplicateKeyException that is defined in non-legacy part of the driver, but is not used.

Generated at Wed Feb 07 21:39:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.