diff --git a/STX.EFxceptions.Cosmos.Base.Tests.Unit/Services/Foundations/CosmosEfxceptionServiceTests.Exceptions.cs b/STX.EFxceptions.Cosmos.Base.Tests.Unit/Services/Foundations/CosmosEfxceptionServiceTests.Exceptions.cs index 6bdd8dd..4b25379 100644 --- a/STX.EFxceptions.Cosmos.Base.Tests.Unit/Services/Foundations/CosmosEfxceptionServiceTests.Exceptions.cs +++ b/STX.EFxceptions.Cosmos.Base.Tests.Unit/Services/Foundations/CosmosEfxceptionServiceTests.Exceptions.cs @@ -4,6 +4,7 @@ using System.Net; using Microsoft.Azure.Cosmos; +using Microsoft.EntityFrameworkCore; using STX.EFxceptions.Cosmos.Base.Models.Exceptions; using Xunit; @@ -18,13 +19,17 @@ public void ShouldThrowCosmosExceptionIfStatusCodeIsNotRecognized() HttpStatusCode cosmosStatusCode = HttpStatusCode.Unused; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then - Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + Assert.Throws(() => + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -34,13 +39,17 @@ public void ShouldThrowAuthenticationFailedCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.Unauthorized; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -50,13 +59,17 @@ public void ShouldThrowAuthorizationFailedCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.Forbidden; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -66,13 +79,17 @@ public void ShouldThrowResourceNotFoundCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.NotFound; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -82,13 +99,17 @@ public void ShouldThrowRequestTimeoutCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.RequestTimeout; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -98,13 +119,17 @@ public void ShouldThrowPreconditionFailedCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.PreconditionFailed; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -114,13 +139,17 @@ public void ShouldThrowPayloadTooLargeCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.RequestEntityTooLarge; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -130,13 +159,17 @@ public void ShouldThrowResourceLockedCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.Locked; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -146,13 +179,17 @@ public void ShouldThrowDependencyFailedCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.FailedDependency; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -162,13 +199,17 @@ public void ShouldThrowTooManyRequestsCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.TooManyRequests; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } [Fact] @@ -178,15 +219,19 @@ public void ShouldThrowInternalServerCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.InternalServerError; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } - + [Fact] public void ShouldThrowServiceUnavailableCosmosException() { @@ -194,13 +239,17 @@ public void ShouldThrowServiceUnavailableCosmosException() HttpStatusCode cosmosStatusCode = HttpStatusCode.ServiceUnavailable; CosmosException cosmosException = CreateCosmosException(cosmosStatusCode); + var dbUpdateException = new DbUpdateException( + message: cosmosException.Message, + innerException: cosmosException); + this.cosmosErrorBrokerMock.Setup(broker => broker.GetErrorCode(cosmosException)) .Returns((int)cosmosStatusCode); // when . then Assert.Throws(() => - this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException)); + this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException)); } } } diff --git a/STX.EFxceptions.Cosmos.Base/STX.EFxceptions.Cosmos.Base.csproj b/STX.EFxceptions.Cosmos.Base/STX.EFxceptions.Cosmos.Base.csproj index ad5f98f..bb0ba98 100644 --- a/STX.EFxceptions.Cosmos.Base/STX.EFxceptions.Cosmos.Base.csproj +++ b/STX.EFxceptions.Cosmos.Base/STX.EFxceptions.Cosmos.Base.csproj @@ -46,7 +46,7 @@ - + diff --git a/STX.EFxceptions.Cosmos.Base/Services/Foundations/CosmosEFxceptionService .cs b/STX.EFxceptions.Cosmos.Base/Services/Foundations/CosmosEFxceptionService .cs index 0a9ff9e..8d59c70 100644 --- a/STX.EFxceptions.Cosmos.Base/Services/Foundations/CosmosEFxceptionService .cs +++ b/STX.EFxceptions.Cosmos.Base/Services/Foundations/CosmosEFxceptionService .cs @@ -2,8 +2,9 @@ // Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers // ---------------------------------------------------------------------------------- +using System; using Microsoft.Azure.Cosmos; -using STX.EFxceptions.Cosmos.Base.Brokers.DbErrorBroker; +using Microsoft.EntityFrameworkCore; using STX.EFxceptions.Interfaces.Brokers.DbErrorBroker; namespace STX.EFxceptions.Cosmos.Base.Services.Foundations @@ -15,11 +16,15 @@ public partial class CosmosEFxceptionService : ICosmosEFxceptionService public CosmosEFxceptionService(IDbErrorBroker cosmosErrorBroker) => this.cosmosErrorBroker = cosmosErrorBroker; - public void ThrowMeaningfulException(CosmosException cosmosException) + public void ThrowMeaningfulException(DbUpdateException dbUpdateException) { + ValidateInnerException(dbUpdateException); + CosmosException cosmosException = GetCosmosException(dbUpdateException.InnerException); int cosmosErrorCode = this.cosmosErrorBroker.GetErrorCode(cosmosException); ConvertAndThrowMeaningfulException(cosmosErrorCode, cosmosException.Message); - throw cosmosException; + throw dbUpdateException; } + + private CosmosException GetCosmosException(Exception exception) => (CosmosException)exception; } } diff --git a/STX.EFxceptions.Cosmos.Base/Services/Foundations/CosmosEFxceptionService.Validations.cs b/STX.EFxceptions.Cosmos.Base/Services/Foundations/CosmosEFxceptionService.Validations.cs new file mode 100644 index 0000000..3ccdb77 --- /dev/null +++ b/STX.EFxceptions.Cosmos.Base/Services/Foundations/CosmosEFxceptionService.Validations.cs @@ -0,0 +1,19 @@ +// ---------------------------------------------------------------------------------- +// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using Microsoft.EntityFrameworkCore; + +namespace STX.EFxceptions.Cosmos.Base.Services.Foundations +{ + public partial class CosmosEFxceptionService + { + private void ValidateInnerException(DbUpdateException dbUpdateException) + { + if (dbUpdateException.InnerException is null) + { + throw dbUpdateException; + } + } + } +} diff --git a/STX.EFxceptions.Cosmos.Base/Services/Foundations/ICosmosEFxceptionService.cs b/STX.EFxceptions.Cosmos.Base/Services/Foundations/ICosmosEFxceptionService.cs index 8748623..84d3349 100644 --- a/STX.EFxceptions.Cosmos.Base/Services/Foundations/ICosmosEFxceptionService.cs +++ b/STX.EFxceptions.Cosmos.Base/Services/Foundations/ICosmosEFxceptionService.cs @@ -2,11 +2,10 @@ // Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Cosmos; using STX.EFxceptions.Interfaces.Services.EFxceptions; namespace STX.EFxceptions.Cosmos.Base.Services.Foundations { - public interface ICosmosEFxceptionService : IEFxceptionService + public interface ICosmosEFxceptionService : IEFxceptionService { } } diff --git a/STX.EFxceptions.Cosmos/EFxceptionsContext.cs b/STX.EFxceptions.Cosmos/EFxceptionsContext.cs index 690a1a5..526de43 100644 --- a/STX.EFxceptions.Cosmos/EFxceptionsContext.cs +++ b/STX.EFxceptions.Cosmos/EFxceptionsContext.cs @@ -25,7 +25,7 @@ protected EFxceptionsContext() protected override IDbErrorBroker CreateErrorBroker() => new CosmosErrorBroker(); - protected override IEFxceptionService CreateEFxceptionService( + protected override IEFxceptionService CreateEFxceptionService( IDbErrorBroker errorBroker) { return new CosmosEFxceptionService(errorBroker); diff --git a/STX.EFxceptions.Cosmos/STX.EFxceptions.Cosmos.csproj b/STX.EFxceptions.Cosmos/STX.EFxceptions.Cosmos.csproj index e5ffe55..6f789b0 100644 --- a/STX.EFxceptions.Cosmos/STX.EFxceptions.Cosmos.csproj +++ b/STX.EFxceptions.Cosmos/STX.EFxceptions.Cosmos.csproj @@ -46,7 +46,7 @@ - + diff --git a/STX.EFxceptions.Identity.Cosmos/EFxceptionsIdentityContext.cs b/STX.EFxceptions.Identity.Cosmos/EFxceptionsIdentityContext.cs index b26f947..fd771b8 100644 --- a/STX.EFxceptions.Identity.Cosmos/EFxceptionsIdentityContext.cs +++ b/STX.EFxceptions.Identity.Cosmos/EFxceptionsIdentityContext.cs @@ -30,9 +30,11 @@ protected EFxceptionsIdentityContext() : base() protected override IDbErrorBroker CreateErrorBroker() => new CosmosErrorBroker(); - protected override IEFxceptionService CreateEFxceptionService( - IDbErrorBroker errorBroker) => - new CosmosEFxceptionService(errorBroker); + protected override IEFxceptionService CreateEFxceptionService( + IDbErrorBroker errorBroker) + { + return new CosmosEFxceptionService(errorBroker); + } } public class EFxceptionsIdentityContext< @@ -57,8 +59,10 @@ protected EFxceptionsIdentityContext() : base() protected override IDbErrorBroker CreateErrorBroker() => new CosmosErrorBroker(); - protected override IEFxceptionService CreateEFxceptionService( - IDbErrorBroker errorBroker) => - new CosmosEFxceptionService(errorBroker); + protected override IEFxceptionService CreateEFxceptionService( + IDbErrorBroker errorBroker) + { + return new CosmosEFxceptionService(errorBroker); + } } } diff --git a/STX.EFxceptions.Identity.Cosmos/STX.EFxceptions.Identity.Cosmos.csproj b/STX.EFxceptions.Identity.Cosmos/STX.EFxceptions.Identity.Cosmos.csproj index 4c2f2f2..bb1064e 100644 --- a/STX.EFxceptions.Identity.Cosmos/STX.EFxceptions.Identity.Cosmos.csproj +++ b/STX.EFxceptions.Identity.Cosmos/STX.EFxceptions.Identity.Cosmos.csproj @@ -47,7 +47,7 @@ - +