Skip to content

Commit c24e868

Browse files
committed
Fix Cosmos update exception wrapping
Fixes #25631
1 parent 213d48f commit c24e868

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/EFCore.Cosmos/Properties/CosmosStrings.resx

+3
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@
265265
<data name="UpdateConflict" xml:space="preserve">
266266
<value>Conflicts were detected for item with id '{itemId}'.</value>
267267
</data>
268+
<data name="UpdateStoreException" xml:space="preserve">
269+
<value>An error occurred while saving the the item with id '{itemId}'. See the inner exception for details.</value>
270+
</data>
268271
<data name="VisitChildrenMustBeOverridden" xml:space="preserve">
269272
<value>'VisitChildren' must be overridden in the class deriving from 'SqlExpression'.</value>
270273
</data>

src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs

+12-18
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ public override int SaveChanges(IList<IUpdateEntry> entries)
111111
rowsAffected++;
112112
}
113113
}
114-
catch (CosmosException ex)
114+
catch (Exception ex) when (ex is not DbUpdateException and not OperationCanceledException)
115115
{
116-
throw ThrowUpdateException(ex, entry);
116+
throw WrapUpdateException(ex, entry);
117117
}
118118
}
119119

@@ -175,9 +175,9 @@ public override async Task<int> SaveChangesAsync(
175175
rowsAffected++;
176176
}
177177
}
178-
catch (CosmosException ex)
178+
catch (Exception ex) when (ex is not DbUpdateException and not OperationCanceledException)
179179
{
180-
throw ThrowUpdateException(ex, entry);
180+
throw WrapUpdateException(ex, entry);
181181
}
182182
}
183183

@@ -378,25 +378,19 @@ private IUpdateEntry GetRootDocument(InternalEntityEntry entry)
378378
}
379379
#pragma warning restore EF1001 // Internal EF Core API usage.
380380

381-
private Exception ThrowUpdateException(CosmosException exception, IUpdateEntry entry)
381+
private Exception WrapUpdateException(Exception exception, IUpdateEntry entry)
382382
{
383383
var documentSource = GetDocumentSource(entry.EntityType);
384384
var id = documentSource.GetId(entry.SharedIdentityEntry ?? entry);
385-
throw exception.StatusCode switch
385+
386+
return exception switch
386387
{
387-
HttpStatusCode.PreconditionFailed =>
388-
new DbUpdateConcurrencyException(CosmosStrings.UpdateConflict(id), exception, new[] { entry }),
389-
HttpStatusCode.Conflict =>
390-
new DbUpdateException(CosmosStrings.UpdateConflict(id), exception, new[] { entry }),
391-
_ => Rethrow(exception),
388+
CosmosException { StatusCode : HttpStatusCode.PreconditionFailed }
389+
=> new DbUpdateConcurrencyException(CosmosStrings.UpdateConflict(id), exception, new[] { entry }),
390+
CosmosException { StatusCode : HttpStatusCode.Conflict }
391+
=> new DbUpdateException(CosmosStrings.UpdateConflict(id), exception, new[] { entry }),
392+
_ => new DbUpdateException(CosmosStrings.UpdateStoreException(id), exception, new[] { entry })
392393
};
393394
}
394-
395-
private static Exception Rethrow(Exception ex)
396-
{
397-
// Re-throw an exception, preserving the original stack and details, without being in the original "catch" block.
398-
ExceptionDispatchInfo.Capture(ex).Throw();
399-
return ex;
400-
}
401395
}
402396
}

0 commit comments

Comments
 (0)