|
| 1 | + |
| 2 | + |
| 3 | +# DbExceptionClassifier |
| 4 | + |
| 5 | +Classify ADO.NET database exceptions by error type. Works with any ADO.NET provider without requiring Entity Framework Core. |
| 6 | + |
| 7 | +Supports **PostgreSQL**, **SQL Server**, **SQLite**, **Oracle**, **MySQL** (MySql.Data and MySqlConnector). |
| 8 | + |
| 9 | +[](License.md) |
| 10 | +[](https://dotnet.microsoft.com/en-us/) |
| 11 | + |
| 12 | +## What does DbExceptionClassifier do? |
| 13 | + |
| 14 | +When working with ADO.NET, database exceptions are provider-specific. To determine whether an error was caused by a unique constraint violation, a foreign key violation, or a null constraint, you need to inspect the provider-specific exception type and error codes. |
| 15 | + |
| 16 | +DbExceptionClassifier provides a unified `IDbExceptionClassifier` interface that classifies `DbException` instances into common error types: |
| 17 | + |
| 18 | +- **Unique constraint violation** |
| 19 | +- **Reference (foreign key) constraint violation** |
| 20 | +- **Cannot insert null** |
| 21 | +- **Max length exceeded** |
| 22 | +- **Numeric overflow** |
| 23 | +- **Deadlock** |
| 24 | + |
| 25 | +## Getting started |
| 26 | + |
| 27 | +Install the package for your database: |
| 28 | + |
| 29 | +``` |
| 30 | +dotnet add package DbExceptionClassifier.PostgreSQL |
| 31 | +``` |
| 32 | + |
| 33 | +``` |
| 34 | +dotnet add package DbExceptionClassifier.SqlServer |
| 35 | +``` |
| 36 | + |
| 37 | +``` |
| 38 | +dotnet add package DbExceptionClassifier.Sqlite.Core |
| 39 | +``` |
| 40 | + |
| 41 | +``` |
| 42 | +dotnet add package DbExceptionClassifier.Oracle |
| 43 | +``` |
| 44 | + |
| 45 | +``` |
| 46 | +dotnet add package DbExceptionClassifier.MySQL |
| 47 | +``` |
| 48 | + |
| 49 | +``` |
| 50 | +dotnet add package DbExceptionClassifier.MySQL.Pomelo |
| 51 | +``` |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +Create an instance of the classifier for your database and use it to classify exceptions: |
| 56 | + |
| 57 | +```csharp |
| 58 | +var classifier = new PostgreSQLExceptionClassifier(); |
| 59 | + |
| 60 | +try |
| 61 | +{ |
| 62 | + // Execute your ADO.NET command |
| 63 | + await command.ExecuteNonQueryAsync(); |
| 64 | +} |
| 65 | +catch (DbException ex) when (classifier.IsUniqueConstraintError(ex)) |
| 66 | +{ |
| 67 | + // Handle unique constraint violation |
| 68 | +} |
| 69 | +catch (DbException ex) when (classifier.IsReferenceConstraintError(ex)) |
| 70 | +{ |
| 71 | + // Handle foreign key violation |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## IDbExceptionClassifier interface |
| 76 | + |
| 77 | +```csharp |
| 78 | +public interface IDbExceptionClassifier |
| 79 | +{ |
| 80 | + bool IsUniqueConstraintError(DbException exception); |
| 81 | + bool IsReferenceConstraintError(DbException exception); |
| 82 | + bool IsCannotInsertNullError(DbException exception); |
| 83 | + bool IsMaxLengthExceededError(DbException exception); |
| 84 | + bool IsNumericOverflowError(DbException exception); |
| 85 | + bool IsDeadlockError(DbException exception); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## CompositeExceptionClassifier |
| 90 | + |
| 91 | +If your application connects to multiple database types, use `CompositeExceptionClassifier` to combine multiple classifiers into one. It delegates to each classifier and returns `true` if any of them matches: |
| 92 | + |
| 93 | +```csharp |
| 94 | +var classifier = new CompositeExceptionClassifier( |
| 95 | + new PostgreSQLExceptionClassifier(), |
| 96 | + new SqlServerExceptionClassifier() |
| 97 | +); |
| 98 | + |
| 99 | +try |
| 100 | +{ |
| 101 | + await command.ExecuteNonQueryAsync(); |
| 102 | +} |
| 103 | +catch (DbException ex) when (classifier.IsUniqueConstraintError(ex)) |
| 104 | +{ |
| 105 | + // Works regardless of which database threw the exception |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Entity Framework Core |
| 110 | + |
| 111 | +If you are using Entity Framework Core, use the [EntityFrameworkCore.Exceptions](https://github.com/Giorgi/EntityFramework.Exceptions) packages instead. They build on top of DbExceptionClassifier and provide typed exceptions that inherit from `DbUpdateException`. |
0 commit comments