Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CODE RUB: Change Type of Exception #18

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Net;
using Microsoft.Azure.Cosmos;
using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.Cosmos.Base.Models.Exceptions;
using Xunit;

Expand All @@ -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<CosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
Assert.Throws<DbUpdateException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<AuthenticationFailedCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<AuthorizationFailedCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<ResourceNotFoundCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<RequestTimeoutCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<PreconditionFailedCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<PayloadTooLargeCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<ResourceLockedCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<DependencyFailedCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -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<TooManyRequestsCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
Expand All @@ -178,29 +219,37 @@ 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<InternalServerCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
public void ShouldThrowServiceUnavailableCosmosException()
{
// given
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<ServiceUnavailableCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(cosmosException));
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
<PackageReference Include="STX.EFxceptions.Interfaces" Version="0.1.0" />
<PackageReference Include="STX.EFxceptions.Interfaces" Version="0.1.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,11 +16,15 @@ public partial class CosmosEFxceptionService : ICosmosEFxceptionService
public CosmosEFxceptionService(IDbErrorBroker<CosmosException> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CosmosException>
public interface ICosmosEFxceptionService : IEFxceptionService
{ }
}
2 changes: 1 addition & 1 deletion STX.EFxceptions.Cosmos/EFxceptionsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected EFxceptionsContext()
protected override IDbErrorBroker<CosmosException> CreateErrorBroker() =>
new CosmosErrorBroker();

protected override IEFxceptionService<CosmosException> CreateEFxceptionService(
protected override IEFxceptionService CreateEFxceptionService(
IDbErrorBroker<CosmosException> errorBroker)
{
return new CosmosEFxceptionService(errorBroker);
Expand Down
2 changes: 1 addition & 1 deletion STX.EFxceptions.Cosmos/STX.EFxceptions.Cosmos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
<PackageReference Include="STX.EFxceptions.Core" Version="0.1.0" />
<PackageReference Include="STX.EFxceptions.Core" Version="0.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 10 additions & 6 deletions STX.EFxceptions.Identity.Cosmos/EFxceptionsIdentityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ protected EFxceptionsIdentityContext() : base()
protected override IDbErrorBroker<CosmosException> CreateErrorBroker() =>
new CosmosErrorBroker();

protected override IEFxceptionService<CosmosException> CreateEFxceptionService(
IDbErrorBroker<CosmosException> errorBroker) =>
new CosmosEFxceptionService(errorBroker);
protected override IEFxceptionService CreateEFxceptionService(
IDbErrorBroker<CosmosException> errorBroker)
{
return new CosmosEFxceptionService(errorBroker);
}
}

public class EFxceptionsIdentityContext<
Expand All @@ -57,8 +59,10 @@ protected EFxceptionsIdentityContext() : base()
protected override IDbErrorBroker<CosmosException> CreateErrorBroker() =>
new CosmosErrorBroker();

protected override IEFxceptionService<CosmosException> CreateEFxceptionService(
IDbErrorBroker<CosmosException> errorBroker) =>
new CosmosEFxceptionService(errorBroker);
protected override IEFxceptionService CreateEFxceptionService(
IDbErrorBroker<CosmosException> errorBroker)
{
return new CosmosEFxceptionService(errorBroker);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.1" />
<PackageReference Include="STX.EFxceptions.Identity.Core" Version="0.1.0" />
<PackageReference Include="STX.EFxceptions.Identity.Core" Version="0.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading