[CSHARP-3272] Don't throw non-specific Exceptions Created: 27/Nov/20 Updated: 27/Oct/23 Resolved: 04/Dec/20 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Error Handling |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Alex Bevilacqua | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 6 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Case: | (copied to CRM) |
| Description |
|
Similar cases have been closed in the past ( For example:
In the above example, when a CStringUtf8Encoding detects a null byte an ArgumentException is raised. As this exception can be raised as any part of a command, to capture context this specific exception would need to be caught and re-thrown. A more appropriate method would be to ensure errors are all enumerated and error codes accessible from the exception instance such as:
This approach is similar to what the server does with error_codes.yml. Note that a search of the mongodb/mongo-csharp-driver for "throw new ArgumentException" provides a number of similar examples. |
| Comments |
| Comment by Robert Stam [ 04/Dec/20 ] |
|
While we agree that examining exception message strings to glean more detail about what happened is fragile (in part because error messages might be reworded) and while we agree that numeric error codes are not a bad idea in theory, we don't think we can change the driver to throw new types of exceptions. That would break any existing application that is catching the existing exceptions. In this particular scenario it seems like the proper application design is to validate the user provided input *before* passing it to the driver rather than passing bad data to the driver and trying to decode the resulting exception. |