From 30fad32794c69572d405a516ac3962d2875bf02c Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Mon, 18 Nov 2024 17:32:21 +1000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20in=20exception=20processor=20?= =?UTF-8?q?for=20strongly=20typed=20SQL=20exeptions=20(#440)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...18-produce-useful-sql-server-exceptions.md | 27 +++++++++++++++++++ src/Infrastructure/DependencyInjection.cs | 5 ++-- src/Infrastructure/Infrastructure.csproj | 3 ++- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 docs/adr/20241118-produce-useful-sql-server-exceptions.md diff --git a/docs/adr/20241118-produce-useful-sql-server-exceptions.md b/docs/adr/20241118-produce-useful-sql-server-exceptions.md new file mode 100644 index 00000000..4b6f0f19 --- /dev/null +++ b/docs/adr/20241118-produce-useful-sql-server-exceptions.md @@ -0,0 +1,27 @@ +# Produce useful SQL Server exceptions + +- Status: approved +- Deciders: Daniel Mackay +- Date: 2024-11-18 +- Tags: ef-core + +Technical Story: https://github.com/SSWConsulting/SSW.CleanArchitecture/issues/408 + +## Context and Problem Statement + +EF Core typically throws `DbUpdateException` which doesn’t tell you much until you drill into the inner exceptions. If you do drill in you will find exceptions specific to the underlying DB Provider. But what if you change provider? Now you need to handle multiple exceptions for the same error. + +## Decision Drivers + +- Produce strongly typed useful exceptions + +## Decision Outcome + +Chosen option: "Option 1", because it does what we need and is far better than the default EF Core exceptions. + +## Pros and Cons of the Options + +### Option 1 - EntityFrameworkCore.Exceptions.SqlServer + +- ✅ Strongly typed exceptions +- ❌ Additional dependency added diff --git a/src/Infrastructure/DependencyInjection.cs b/src/Infrastructure/DependencyInjection.cs index 0177bc44..1cf90131 100644 --- a/src/Infrastructure/DependencyInjection.cs +++ b/src/Infrastructure/DependencyInjection.cs @@ -1,3 +1,4 @@ +using EntityFramework.Exceptions.SqlServer; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SSW.CleanArchitecture.Application.Common.Interfaces; @@ -19,8 +20,8 @@ public static void AddInfrastructure(this IHostApplicationBuilder builder) serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService()); - // TODO: Add this - // options.UseExceptionProcessor(); + // Return strongly typed useful exceptions + options.UseExceptionProcessor(); }); var services = builder.Services; diff --git a/src/Infrastructure/Infrastructure.csproj b/src/Infrastructure/Infrastructure.csproj index 6524007a..98d2b92a 100644 --- a/src/Infrastructure/Infrastructure.csproj +++ b/src/Infrastructure/Infrastructure.csproj @@ -14,7 +14,8 @@ - + +