Skip to content

Commit

Permalink
Merge pull request #19 from The-Standard-Organization/users/ZafarUrak…
Browse files Browse the repository at this point in the history
…ov/coderub-add-new-type-exception

CODE RUB: Handle New Type of Exception
  • Loading branch information
cjdutoit authored Jan 31, 2024
2 parents a68f919 + 5cbca28 commit 5ed7400
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ public void ShouldThrowCosmosExceptionIfStatusCodeIsNotRecognized()
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
public void ShouldThrowDuplicateKeyCosmosException()
{
// given
HttpStatusCode cosmosStatusCode = HttpStatusCode.Conflict;
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<DuplicateKeyCosmosException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));
}

[Fact]
public void ShouldThrowAuthenticationFailedCosmosException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Cosmos;
using Microsoft.EntityFrameworkCore;
using Moq;
using System;
using Xunit;

namespace STX.EFxceptions.Cosmos.Base.Tests.Unit.Services.Foundations
{
public partial class CosmosEfxceptionServiceTests
{
[Fact]
public void ShouldThrowDbUpdateExceptionIfCosmosExceptionWasNull()
{
// given
var dbUpdateException = new DbUpdateException(null, default(Exception));

// when . then
Assert.Throws<DbUpdateException>(() =>
this.cosmosEFxceptionService.ThrowMeaningfulException(dbUpdateException));

this.cosmosErrorBrokerMock.Verify(broker =>
broker.GetErrorCode(It.IsAny<CosmosException>()),
Times.Never);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;

namespace STX.EFxceptions.Cosmos.Base.Models.Exceptions
{
public class DuplicateKeyCosmosException : Exception
{
public DuplicateKeyCosmosException(string message) : base(message) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ private void ConvertAndThrowMeaningfulException(int cosmosErrorCode, string mess
throw new ResourceNotFoundCosmosException(message);
case 408:
throw new RequestTimeoutCosmosException(message);
case 409:
throw new DuplicateKeyCosmosException(message);
case 412:
throw new PreconditionFailedCosmosException(message);
case 413:
Expand Down
2 changes: 1 addition & 1 deletion STX.EFxceptions.Cosmos/EFxceptionsContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public EFxceptionsIdentityContext(DbContextOptions options) : base(options)

protected EFxceptionsIdentityContext() : base()
{ }

protected override IDbErrorBroker<CosmosException> CreateErrorBroker() =>
new CosmosErrorBroker();

Expand Down

0 comments on commit 5ed7400

Please sign in to comment.