Skip to content

Commit ba5563b

Browse files
committed
Add support for analyzing plain ADO.NET exceptions
1 parent b922986 commit ba5563b

File tree

50 files changed

+455
-305
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+455
-305
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Data.Common;
2+
3+
namespace DbExceptionClassifier.Common
4+
{
5+
public interface IDbExceptionClassifier
6+
{
7+
public bool IsReferenceConstraintError(DbException exception);
8+
public bool IsCannotInsertNullError(DbException exception);
9+
public bool IsNumericOverflowError(DbException exception);
10+
public bool IsUniqueConstraintError(DbException exception);
11+
public bool IsMaxLengthExceededError(DbException exception);
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project>
2+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
3+
4+
<PropertyGroup Label="Configure assembly names and namespaces">
5+
<AssemblyName>DbExceptionClassifier.$(MSBuildProjectName)</AssemblyName>
6+
<RootNamespace>$(AssemblyName)</RootNamespace>
7+
<PackageId>DbExceptionClassifier.$(MSBuildProjectName)</PackageId>
8+
</PropertyGroup>
9+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
10+
<DefineConstants>TRACE;POMELO</DefineConstants>
11+
</PropertyGroup>
12+
13+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
14+
<DefineConstants>TRACE;POMELO</DefineConstants>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<Compile Include="..\MySQL\MySQLExceptionClassifier.cs" Link="MySQLExceptionClassifier.cs" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Include="MySqlConnector" Version="2.4.0" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="MySql.Data" Version="8.3.0" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Data.Common;
2+
using DbExceptionClassifier.Common;
3+
4+
#if POMELO
5+
using MySqlConnector;
6+
namespace DbExceptionClassifier.MySQL.Pomelo;
7+
#else
8+
using MySql.Data.MySqlClient;
9+
namespace DbExceptionClassifier.MySQL;
10+
#endif
11+
12+
13+
public class MySQLExceptionClassifier : IDbExceptionClassifier
14+
{
15+
private static MySqlErrorCode GetErrorCode(DbException dbException)
16+
{
17+
if (dbException is not MySqlException mySqlException)
18+
{
19+
return MySqlErrorCode.None;
20+
}
21+
22+
#if POMELO
23+
return mySqlException.ErrorCode;
24+
#else
25+
return (MySqlErrorCode)mySqlException.Number;
26+
#endif
27+
}
28+
29+
public bool IsReferenceConstraintError(DbException exception)
30+
{
31+
var errorCode = GetErrorCode(exception);
32+
33+
return errorCode is MySqlErrorCode.NoReferencedRow or
34+
MySqlErrorCode.RowIsReferenced or
35+
MySqlErrorCode.NoReferencedRow2 or
36+
MySqlErrorCode.RowIsReferenced2;
37+
}
38+
39+
public bool IsCannotInsertNullError(DbException exception)
40+
{
41+
var errorCode = GetErrorCode(exception);
42+
return errorCode == MySqlErrorCode.ColumnCannotBeNull;
43+
}
44+
45+
public bool IsNumericOverflowError(DbException exception)
46+
{
47+
var errorCode = GetErrorCode(exception);
48+
return errorCode == MySqlErrorCode.WarningDataOutOfRange;
49+
}
50+
51+
public bool IsUniqueConstraintError(DbException exception)
52+
{
53+
var errorCode = GetErrorCode(exception);
54+
return errorCode == MySqlErrorCode.DuplicateKeyEntry;
55+
}
56+
57+
public bool IsMaxLengthExceededError(DbException exception)
58+
{
59+
var errorCode = GetErrorCode(exception);
60+
return errorCode == MySqlErrorCode.DataTooLong;
61+
}
62+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="3.21.120" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Data.Common;
2+
using DbExceptionClassifier.Common;
3+
using Oracle.ManagedDataAccess.Client;
4+
5+
namespace DbExceptionClassifier.Oracle;
6+
7+
public class OracleExceptionClassifier : IDbExceptionClassifier
8+
{
9+
private const int CannotInsertNull = 1400;
10+
private const int CannotUpdateToNull = 1407;
11+
private const int UniqueConstraintViolation = 1;
12+
private const int IntegrityConstraintViolation = 2291;
13+
private const int ChildRecordFound = 2292;
14+
private const int NumericOverflow = 1438;
15+
private const int NumericOrValueError = 12899;
16+
17+
public bool IsReferenceConstraintError(DbException exception) => exception is OracleException { Number: IntegrityConstraintViolation or ChildRecordFound };
18+
19+
public bool IsCannotInsertNullError(DbException exception) => exception is OracleException { Number: CannotInsertNull or CannotUpdateToNull };
20+
21+
public bool IsNumericOverflowError(DbException exception) => exception is OracleException { Number: NumericOverflow };
22+
23+
public bool IsUniqueConstraintError(DbException exception) => exception is OracleException { Number: UniqueConstraintViolation };
24+
25+
public bool IsMaxLengthExceededError(DbException exception) => exception is OracleException { Number: NumericOrValueError };
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Npgsql" Version="8.0.6" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using DbExceptionClassifier.Common;
2+
using Npgsql;
3+
using System.Data.Common;
4+
5+
namespace DbExceptionClassifier.PostgreSQL;
6+
7+
public class PostgreSQLExceptionClassifier : IDbExceptionClassifier
8+
{
9+
public bool IsReferenceConstraintError(DbException exception) => exception is PostgresException { SqlState: PostgresErrorCodes.ForeignKeyViolation };
10+
public bool IsCannotInsertNullError(DbException exception) => exception is PostgresException { SqlState: PostgresErrorCodes.NotNullViolation };
11+
public bool IsNumericOverflowError(DbException exception) => exception is PostgresException { SqlState: PostgresErrorCodes.NumericValueOutOfRange };
12+
public bool IsUniqueConstraintError(DbException exception) => exception is PostgresException { SqlState: PostgresErrorCodes.UniqueViolation };
13+
public bool IsMaxLengthExceededError(DbException exception) => exception is PostgresException { SqlState: PostgresErrorCodes.StringDataRightTruncation };
14+
}

0 commit comments

Comments
 (0)