private async ValueTask <Contact> TryCatch(ReturningContactFunction returningContactFunction)
        {
            try
            {
                return(await returningContactFunction());
            }
            catch (NullContactException nullContactException)
            {
                throw CreateAndLogValidationException(nullContactException);
            }
            catch (NotFoundContactException notFoundContactException)
            {
                throw CreateAndLogValidationException(notFoundContactException);
            }
            catch (InvalidContactException invalidContactException)
            {
                throw CreateAndLogValidationException(invalidContactException);
            }
            catch (SqlException sqlException)
            {
                var failedContactStorageException =
                    new FailedContactStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedContactStorageException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsContactException =
                    new AlreadyExistsContactException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsContactException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedContactException =
                    new LockedContactException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedContactException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedContactServiceException =
                    new FailedContactServiceException(exception);

                throw CreateAndLogServiceException(failedContactServiceException);
            }
        }
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Contact        randomContact        = CreateRandomContact(randomDateTime);
            Contact        someContact          = randomContact;

            someContact.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedContactException             = new LockedContactException(databaseUpdateConcurrencyException);

            var expectedContactDependencyException =
                new ContactDependencyException(lockedContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(someContact.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDateTime);

            // when
            ValueTask <Contact> modifyContactTask =
                this.contactService.ModifyContactAsync(someContact);

            // then
            await Assert.ThrowsAsync <ContactDependencyException>(() =>
                                                                  modifyContactTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectContactByIdAsync(someContact.Id),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedContactDependencyException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomContactId = Guid.NewGuid();
            Guid inputContactId  = randomContactId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var  lockedContactException             = new LockedContactException(databaseUpdateConcurrencyException);

            var expectedContactDependencyException =
                new ContactDependencyException(lockedContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(inputContactId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Contact> deleteContactTask =
                this.contactService.RemoveContactByIdAsync(inputContactId);

            // then
            await Assert.ThrowsAsync <ContactDependencyException>(() =>
                                                                  deleteContactTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedContactDependencyException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectContactByIdAsync(inputContactId),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.DeleteContactAsync(It.IsAny <Contact>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }