private async ValueTask <GuardianContact> TryCatch( ReturningGuardianContactFunction returningGuardianContactFunction) { try { return(await returningGuardianContactFunction()); } catch (NullGuardianContactException nullGuardianContactException) { throw CreateAndLogValidationException(nullGuardianContactException); } catch (InvalidGuardianContactException invalidGuardianContactException) { throw CreateAndLogValidationException(invalidGuardianContactException); } catch (NotFoundGuardianContactException notFoundGuardianContactException) { throw CreateAndLogValidationException(notFoundGuardianContactException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsGuardianContactException = new AlreadyExistsGuardianContactException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsGuardianContactException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidGuardianContactReferenceException = new InvalidGuardianContactReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidGuardianContactReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedGuardianContactException = new LockedGuardianContactException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedGuardianContactException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedGuardianContactServiceException = new FailedGuardianContactServiceException(exception); throw CreateAndLogServiceException(failedGuardianContactServiceException); } }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given var randomContactId = Guid.NewGuid(); var randomGuardianId = Guid.NewGuid(); Guid someContactId = randomContactId; Guid someGuardianId = randomGuardianId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedContactException = new LockedGuardianContactException(databaseUpdateConcurrencyException); var expectedGuardianContactException = new GuardianContactDependencyException(lockedContactException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <GuardianContact> removeGuardianContactTask = this.guardianContactService.RemoveGuardianContactByIdAsync(someGuardianId, someContactId); // then await Assert.ThrowsAsync <GuardianContactDependencyException>(() => removeGuardianContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianContactException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianContactByIdAsync(someGuardianId, someContactId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianContactAsync(It.IsAny <GuardianContact>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }