Skip to content

Commit

Permalink
Extract common code
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Aug 6, 2024
1 parent c1bf26d commit 0e12953
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions EntityFramework.Exceptions.Common/ExceptionProcessorInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,27 +31,7 @@ public override void SaveChangesFailed(DbContextErrorEventData eventData)
{
var dbUpdateException = eventData.Exception as DbUpdateException;

if (eventData.Exception.GetBaseException() is T providerException)
{
var error = GetDatabaseError(providerException);

if (error != null && dbUpdateException != null)
{
var exception = ExceptionFactory.Create(error.Value, dbUpdateException, dbUpdateException.Entries);

switch (exception)
{
case UniqueConstraintException uniqueConstraint when eventData.Context != null:
SetConstraintDetails(eventData.Context, uniqueConstraint, providerException);
break;
case ReferenceConstraintException referenceConstraint when eventData.Context != null:
SetConstraintDetails(eventData.Context, referenceConstraint, providerException);
break;
}

throw exception;
}
}
ProcessException(eventData, dbUpdateException);

base.SaveChangesFailed(eventData);
}
Expand All @@ -60,6 +41,14 @@ public override void SaveChangesFailed(DbContextErrorEventData eventData)
{
var dbUpdateException = eventData.Exception as DbUpdateException;

ProcessException(eventData, dbUpdateException);

return base.SaveChangesFailedAsync(eventData, cancellationToken);
}

[StackTraceHidden]
private void ProcessException(DbContextErrorEventData eventData, DbUpdateException dbUpdateException)
{
if (eventData.Exception.GetBaseException() is T providerException)
{
var error = GetDatabaseError(providerException);
Expand All @@ -77,15 +66,11 @@ public override void SaveChangesFailed(DbContextErrorEventData eventData)
SetConstraintDetails(eventData.Context, referenceConstraint, providerException);
break;
}

throw exception;
}
}

return base.SaveChangesFailedAsync(eventData, cancellationToken);
}


private void SetConstraintDetails(DbContext context, UniqueConstraintException exception, Exception providerException)
{
if (uniqueIndexDetailsList == null)
Expand Down

0 comments on commit 0e12953

Please sign in to comment.