Skip to content

Commit 12f434a

Browse files
committed
Consolidate into one ProcessException() method
1 parent ac24f9e commit 12f434a

File tree

2 files changed

+25
-54
lines changed

2 files changed

+25
-54
lines changed

EntityFramework.Exceptions.Common/ExceptionFactory.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.EntityFrameworkCore;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Data.Common;
54
using Microsoft.EntityFrameworkCore.ChangeTracking;
@@ -10,24 +9,19 @@ static class ExceptionFactory
109
{
1110
internal static Exception Create<T>(ExceptionProcessorInterceptor<T>.DatabaseError error, Exception exception, IReadOnlyList<EntityEntry> entries) where T : DbException
1211
{
13-
return error switch
12+
if (entries?.Count > 0)
1413
{
15-
ExceptionProcessorInterceptor<T>.DatabaseError.CannotInsertNull when entries.Count > 0 => new CannotInsertNullException("Cannot insert null", exception.InnerException, entries),
16-
ExceptionProcessorInterceptor<T>.DatabaseError.CannotInsertNull when entries.Count == 0 => new CannotInsertNullException("Cannot insert null", exception.InnerException),
17-
ExceptionProcessorInterceptor<T>.DatabaseError.MaxLength when entries.Count > 0 => new MaxLengthExceededException("Maximum length exceeded", exception.InnerException, entries),
18-
ExceptionProcessorInterceptor<T>.DatabaseError.MaxLength when entries.Count == 0 => new MaxLengthExceededException("Maximum length exceeded", exception.InnerException),
19-
ExceptionProcessorInterceptor<T>.DatabaseError.NumericOverflow when entries.Count > 0 => new NumericOverflowException("Numeric overflow", exception.InnerException, entries),
20-
ExceptionProcessorInterceptor<T>.DatabaseError.NumericOverflow when entries.Count == 0 => new NumericOverflowException("Numeric overflow", exception.InnerException),
21-
ExceptionProcessorInterceptor<T>.DatabaseError.ReferenceConstraint when entries.Count > 0 => new ReferenceConstraintException("Reference constraint violation", exception.InnerException, entries),
22-
ExceptionProcessorInterceptor<T>.DatabaseError.ReferenceConstraint when entries.Count == 0 => new ReferenceConstraintException("Reference constraint violation", exception.InnerException),
23-
ExceptionProcessorInterceptor<T>.DatabaseError.UniqueConstraint when entries.Count > 0 => new UniqueConstraintException("Unique constraint violation", exception.InnerException, entries),
24-
ExceptionProcessorInterceptor<T>.DatabaseError.UniqueConstraint when entries.Count == 0 => new UniqueConstraintException("Unique constraint violation", exception.InnerException),
25-
_ => null,
26-
};
27-
}
28-
29-
internal static Exception Create<T>(ExceptionProcessorInterceptor<T>.DatabaseError error, Exception exception) where T : DbException
30-
{
14+
return error switch
15+
{
16+
ExceptionProcessorInterceptor<T>.DatabaseError.CannotInsertNull => new CannotInsertNullException( "Cannot insert null", exception.InnerException, entries),
17+
ExceptionProcessorInterceptor<T>.DatabaseError.MaxLength => new MaxLengthExceededException( "Maximum length exceeded", exception.InnerException, entries),
18+
ExceptionProcessorInterceptor<T>.DatabaseError.NumericOverflow => new NumericOverflowException( "Numeric overflow", exception.InnerException, entries),
19+
ExceptionProcessorInterceptor<T>.DatabaseError.ReferenceConstraint => new ReferenceConstraintException( "Reference constraint violation", exception.InnerException, entries),
20+
ExceptionProcessorInterceptor<T>.DatabaseError.UniqueConstraint => new UniqueConstraintException( "Unique constraint violation", exception.InnerException, entries),
21+
_ => null,
22+
};
23+
}
24+
3125
return error switch
3226
{
3327
ExceptionProcessorInterceptor<T>.DatabaseError.CannotInsertNull => new CannotInsertNullException("Cannot insert null", exception.InnerException),

EntityFramework.Exceptions.Common/ExceptionProcessorInterceptor.cs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,75 +28,52 @@ protected internal enum DatabaseError
2828
/// <inheritdoc />
2929
public void SaveChangesFailed(DbContextErrorEventData eventData)
3030
{
31-
ProcessDbUpdateException(eventData, eventData.Exception as DbUpdateException);
31+
ProcessException(eventData.Exception, eventData.Context);
3232
}
3333

3434
/// <inheritdoc />
3535
public Task SaveChangesFailedAsync(DbContextErrorEventData eventData,
3636
CancellationToken cancellationToken = new CancellationToken())
3737
{
38-
ProcessDbUpdateException(eventData, eventData.Exception as DbUpdateException);
38+
ProcessException(eventData.Exception, eventData.Context);
3939
return Task.CompletedTask;
4040
}
4141

4242
/// <inheritdoc />
4343
public void CommandFailed(DbCommand command, CommandErrorEventData eventData)
4444
{
45-
ProcessDbException(eventData, eventData.Exception as DbException);
45+
ProcessException(eventData.Exception, eventData.Context);
4646
}
4747

4848
/// <inheritdoc />
4949
public Task CommandFailedAsync(DbCommand command, CommandErrorEventData eventData,
5050
CancellationToken cancellationToken = new CancellationToken())
5151
{
52-
ProcessDbException(eventData, eventData.Exception as DbException);
52+
ProcessException(eventData.Exception, eventData.Context);
5353
return Task.CompletedTask;
5454
}
5555

5656
protected abstract DatabaseError? GetDatabaseError(TProviderException dbException);
5757

5858
[StackTraceHidden]
59-
private void ProcessDbUpdateException(DbContextErrorEventData eventData, DbUpdateException dbUpdateException)
59+
private void ProcessException(Exception eventException, DbContext eventContext)
6060
{
61-
if (dbUpdateException == null || eventData.Exception.GetBaseException() is not TProviderException providerException) return;
61+
if (eventException?.GetBaseException() is not TProviderException providerException) return;
6262

6363
var error = GetDatabaseError(providerException);
6464

6565
if (error == null) return;
6666

67-
var exception = ExceptionFactory.Create(error.Value, dbUpdateException, dbUpdateException.Entries);
67+
var updateException = eventException as DbUpdateException;
68+
var exception = ExceptionFactory.Create(error.Value, eventException, updateException?.Entries);
6869

6970
switch (exception)
7071
{
71-
case UniqueConstraintException uniqueConstraint when eventData.Context != null:
72-
SetConstraintDetails(eventData.Context, uniqueConstraint, providerException);
72+
case UniqueConstraintException uniqueConstraint when eventContext != null:
73+
SetConstraintDetails(eventContext, uniqueConstraint, providerException);
7374
break;
74-
case ReferenceConstraintException referenceConstraint when eventData.Context != null:
75-
SetConstraintDetails(eventData.Context, referenceConstraint, providerException);
76-
break;
77-
}
78-
79-
throw exception;
80-
}
81-
82-
[StackTraceHidden]
83-
private void ProcessDbException(CommandErrorEventData eventData, DbException dbException)
84-
{
85-
if (dbException == null || eventData.Exception.GetBaseException() is not TProviderException providerException) return;
86-
87-
var error = GetDatabaseError(providerException);
88-
89-
if (error == null) return;
90-
91-
var exception = ExceptionFactory.Create(error.Value, dbException);
92-
93-
switch (exception)
94-
{
95-
case UniqueConstraintException uniqueConstraint when eventData.Context != null:
96-
SetConstraintDetails(eventData.Context, uniqueConstraint, providerException);
97-
break;
98-
case ReferenceConstraintException referenceConstraint when eventData.Context != null:
99-
SetConstraintDetails(eventData.Context, referenceConstraint, providerException);
75+
case ReferenceConstraintException referenceConstraint when eventContext != null:
76+
SetConstraintDetails(eventContext, referenceConstraint, providerException);
10077
break;
10178
}
10279

0 commit comments

Comments
 (0)