From 34bc293c73dd2d06d2e40560aad0b65a15dfc748 Mon Sep 17 00:00:00 2001 From: Dion Date: Sun, 3 Nov 2024 12:06:27 +0100 Subject: [PATCH] try to catch innerException T:Microsoft.EntityFrameworkCore.DbUpdateException M:An error occurred while saving the entity changes. See the inner exception for details. I: MySqlConnector.MySqlException (0x80004005): Duplicate entry 'OTO4ONBELG2KA4GQ7VNKTJESIM' for key 'PRIMARY' --- .../starsky.foundation.database/Thumbnails/ThumbnailQuery.cs | 5 +++-- .../Thumbnails/ThumbnailQueryErrorTest.cs | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/starsky/starsky.foundation.database/Thumbnails/ThumbnailQuery.cs b/starsky/starsky.foundation.database/Thumbnails/ThumbnailQuery.cs index ef942e99e..366cd2b1e 100644 --- a/starsky/starsky.foundation.database/Thumbnails/ThumbnailQuery.cs +++ b/starsky/starsky.foundation.database/Thumbnails/ThumbnailQuery.cs @@ -283,7 +283,7 @@ private async Task SaveChangesDuplicate(DbContext dbContext) catch ( Exception exception ) { // Check if the inner exception is a MySqlException - var mySqlException = exception as MySqlException; + var mySqlException = exception.InnerException as MySqlException; // Skip if Duplicate entry // MySqlConnector.MySqlException (0x80004005): Duplicate entry for key 'PRIMARY' // https://github.com/qdraw/starsky/issues/1248 https://github.com/qdraw/starsky/issues/1489 @@ -299,7 +299,8 @@ private async Task SaveChangesDuplicate(DbContext dbContext) _logger.LogError($"[SaveChangesDuplicate] T:{exception.GetType()} " + $"M:{exception.Message} " + $"I: {exception.InnerException} " + - $"ErrorCode: {mySqlException?.ErrorCode}"); + $"IM: {mySqlException?.Message} " + + $"IM ErrorCode: {mySqlException?.ErrorCode}"); throw; } diff --git a/starsky/starskytest/starsky.foundation.database/Thumbnails/ThumbnailQueryErrorTest.cs b/starsky/starskytest/starsky.foundation.database/Thumbnails/ThumbnailQueryErrorTest.cs index 7c0b7a92e..6ac77eab4 100644 --- a/starsky/starskytest/starsky.foundation.database/Thumbnails/ThumbnailQueryErrorTest.cs +++ b/starsky/starskytest/starsky.foundation.database/Thumbnails/ThumbnailQueryErrorTest.cs @@ -108,8 +108,7 @@ public async Task AddThumbnailRangeAsync_SomethingElseShould_ExpectedException() new FakeIWebLogger(), new FakeMemoryCache() ); - // Assert that a MySqlException is thrown when AddThumbnailRangeAsync is called - await Assert.ThrowsExceptionAsync(async () => + await Assert.ThrowsExceptionAsync(async () => await fakeQuery.AddThumbnailRangeAsync([new ThumbnailResultDataTransferModel("t")])); } @@ -252,7 +251,7 @@ public MySqlSaveDbExceptionContext(DbContextOptions options, string error, public override Task SaveChangesAsync(CancellationToken cancellationToken = default) { IsCalledMySqlSaveDbExceptionContext = true; - throw CreateMySqlException(_error, _key); + throw new DbUpdateException("dbUpdate", CreateMySqlException(_error, _key)); } [SuppressMessage("Usage",