@@ -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