Skip to content

Commit

Permalink
Merge pull request #43 from The-Standard-Organization/users/ZafarUrak…
Browse files Browse the repository at this point in the history
…ov/code-rub-try-catch-add

CODE RUB: Unclude TryCatch
  • Loading branch information
cjdutoit authored Feb 26, 2024
2 parents 28fcc3c + de02966 commit deca5a6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using Microsoft.Azure.Cosmos;
using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.Abstractions.Models.Exceptions;
using STX.EFxceptions.Cosmos.Base.Models.Exceptions;
using Xunit;

Expand Down Expand Up @@ -48,7 +49,7 @@ public void ShouldThrowDuplicateKeyCosmosException()
.Returns((int)cosmosStatusCode);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ public partial class CosmosEFxceptionService : ICosmosEFxceptionService
public CosmosEFxceptionService(IDbErrorBroker<CosmosException> cosmosErrorBroker) =>
this.cosmosErrorBroker = cosmosErrorBroker;

public void ThrowMeaningfulException(DbUpdateException dbUpdateException)
public void ThrowMeaningfulException(DbUpdateException dbUpdateException) =>
TryCatch(() =>
{
ValidateInnerException(dbUpdateException);
CosmosException cosmosException = GetCosmosException(dbUpdateException.InnerException);
int cosmosErrorCode = this.cosmosErrorBroker.GetErrorCode(cosmosException);
ConvertAndThrowMeaningfulException(cosmosErrorCode, cosmosException.Message);
throw dbUpdateException;
}
});

private CosmosException GetCosmosException(Exception exception) => (CosmosException)exception;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.Abstractions.Models.Exceptions;
using STX.EFxceptions.Cosmos.Base.Models.Exceptions;

namespace STX.EFxceptions.Cosmos.Base.Services.Foundations
{
public partial class CosmosEFxceptionService
{
public delegate void ReturningExceptionFunction();

public void TryCatch(ReturningExceptionFunction returningExceptionFunction)
{
try
{
returningExceptionFunction();
}
catch (DuplicateKeyCosmosException duplicateKeyCosmosException)
{
throw new DuplicateKeyException(
message: duplicateKeyCosmosException.Message,
innerException: duplicateKeyCosmosException);
}
catch (DbUpdateException)
{
throw;
}
}

private void ConvertAndThrowMeaningfulException(int cosmosErrorCode, string message)
{
switch (cosmosErrorCode)
Expand Down

0 comments on commit deca5a6

Please sign in to comment.