Skip to content

Commit d585893

Browse files
author
Bart Koelman
committed
The exception thrown and wrapped on SaveChanges failure was superseeded while disposing PlaceholderResourceCollector that threw too, resulting in a 500 error response. And it produced noisy logs in cibuild, coming from ExceptionHandler. This commit swallows the second exception so the original one can propagate through the pipeline as intended.
1 parent ceedaa8 commit d585893

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ protected virtual async Task SaveChangesAsync(CancellationToken cancellationToke
528528
{
529529
await _dbContext.SaveChangesAsync(cancellationToken);
530530
}
531-
catch (DbUpdateException exception)
531+
catch (Exception exception) when (exception is DbUpdateException || exception is InvalidOperationException)
532532
{
533533
if (_dbContext.Database.CurrentTransaction != null)
534534
{

src/JsonApiDotNetCore/Repositories/PlaceholderResourceCollector.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ private void Detach(IEnumerable<object> resources)
7070
{
7171
foreach (object resource in resources)
7272
{
73-
_dbContext.Entry(resource).State = EntityState.Detached;
73+
try
74+
{
75+
_dbContext.Entry(resource).State = EntityState.Detached;
76+
}
77+
catch (InvalidOperationException)
78+
{
79+
// If SaveChanges() threw due to a foreign key constraint violation, its exception is rethrown here.
80+
// We swallow this exception, to allow the originating error to propagate.
81+
}
7482
}
7583
}
7684
}

0 commit comments

Comments
 (0)