Skip to content

Commit

Permalink
✨ Add in exception processor for strongly typed SQL exeptions (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmackay authored Nov 18, 2024
1 parent ae77958 commit 30fad32
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
27 changes: 27 additions & 0 deletions docs/adr/20241118-produce-useful-sql-server-exceptions.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions src/Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using EntityFramework.Exceptions.SqlServer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SSW.CleanArchitecture.Application.Common.Interfaces;
Expand All @@ -19,8 +20,8 @@ public static void AddInfrastructure(this IHostApplicationBuilder builder)
serviceProvider.GetRequiredService<EntitySaveChangesInterceptor>(),
serviceProvider.GetRequiredService<DispatchDomainEventsInterceptor>());

// TODO: Add this
// options.UseExceptionProcessor();
// Return strongly typed useful exceptions
options.UseExceptionProcessor();
});

var services = builder.Services;
Expand Down
3 changes: 2 additions & 1 deletion src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
<FrameworkReference Include="Microsoft.AspNetCore.App"/>

<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />

<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.3" />

<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
Expand Down

0 comments on commit 30fad32

Please sign in to comment.