[CSHARP-4812] Improve docs or improve duplicate key error handling Created: 16/Oct/23 Updated: 07/Dec/23 |
|
| Status: | Backlog |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | 3.0.0 |
| Type: | Improvement | Priority: | Unknown |
| Reporter: | Elijah Lopez | Assignee: | Oleksandr Poliakov |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
So when I first wrote my code for signing up a user, I had written this in case a user tried to sign up from two phones at the same time. The code is below try { await _usersCollection.InsertOneAsync(user); return true; }catch (MongoDuplicateKeyException) { return false; } Yesterday, I had to create a custom account for the mobile app reviewer so that they don't have to use their own email. The review failed. I wondered why today. I looked at the logs, ran the test myself, and at first it worked and then on subsequent tries it did not. I looked at the logs and saw duplicate key error. I saw my source code it was as seen above. I was confused. Miraculously, I found this on stack overflow so I was able to fix my code even though the fault was MongoDB for having an exception called DuplicateKeyException that doesn't actually catch a duplicate key exception. } catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) { return false; }I suggest that the following is documented as an example in the API docs or in the usage docs or better yet, I hope the driver is updated so that the code in the first block works because the second block is too verbose to know by heart. |
| Comments |
| Comment by Oleksandr Poliakov [ 07/Dec/23 ] |
|
Sorry for the really late response. Thank you for reporting this non-intuitive behavior. I agree that exception handling ought to be reviewed, but we cannot do this just now as it will lead to a breaking change. We will include such review into upcoming v3 release where we planning to spend most of our time on API review and polishing.
Thanks |
| Comment by PM Bot [ 16/Oct/23 ] |
|
Hi elopez@splitthetank.com, thank you for reporting this issue! The team will look into it and get back to you soon. |