コード例 #1
0
        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);
            }
        }
コード例 #2
0
        public async void ShouldThrowValidationExceptionOnAddWhenGuardianContactAlreadyExistsAndLogItAsync()
        {
            // given
            GuardianContact randomGuardianContact        = CreateRandomGuardianContact();
            GuardianContact alreadyExistsGuardianContact = randomGuardianContact;
            string          randomMessage         = GetRandomMessage();
            string          exceptionMessage      = randomMessage;
            var             duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsGuardianContactException =
                new AlreadyExistsGuardianContactException(duplicateKeyException);

            var expectedGuardianContactValidationException =
                new GuardianContactValidationException(alreadyExistsGuardianContactException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertGuardianContactAsync(alreadyExistsGuardianContact))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <GuardianContact> addGuardianContactTask =
                this.guardianContactService.AddGuardianContactAsync(alreadyExistsGuardianContact);

            // then
            await Assert.ThrowsAsync <GuardianContactValidationException>(() =>
                                                                          addGuardianContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertGuardianContactAsync(alreadyExistsGuardianContact),
                                          Times.Once);

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