コード例 #1
0
        private async ValueTask <TeacherContact> TryCatch(
            ReturningTeacherContactFunction returningTeacherContactFunction)
        {
            try
            {
                return(await returningTeacherContactFunction());
            }
            catch (NullTeacherContactException nullTeacherContactException)
            {
                throw CreateAndLogValidationException(nullTeacherContactException);
            }
            catch (InvalidTeacherContactException invalidTeacherContactException)
            {
                throw CreateAndLogValidationException(invalidTeacherContactException);
            }
            catch (NotFoundTeacherContactException notFoundTeacherContactException)
            {
                throw CreateAndLogValidationException(notFoundTeacherContactException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsTeacherContactException =
                    new AlreadyExistsTeacherContactException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsTeacherContactException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidTeacherContactReferenceException =
                    new InvalidTeacherContactReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidTeacherContactReferenceException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedTeacherContactException =
                    new LockedTeacherContactException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedTeacherContactException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedTeacherContactServiceException =
                    new FailedTeacherContactServiceException(exception);

                throw CreateAndLogServiceException(failedTeacherContactServiceException);
            }
        }
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            TeacherContact randomTeacherContact  = CreateRandomTeacherContact();
            TeacherContact invalidTeacherContact = randomTeacherContact;
            string         randomMessage         = GetRandomMessage();
            string         exceptionMessage      = randomMessage;
            var            foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidTeacherContactReferenceException =
                new InvalidTeacherContactReferenceException(foreignKeyConstraintConflictException);

            var expectedTeacherContactValidationException =
                new TeacherContactValidationException(invalidTeacherContactReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherContactAsync(invalidTeacherContact))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <TeacherContact> addTeacherContactTask =
                this.teacherContactService.AddTeacherContactAsync(invalidTeacherContact);

            // then
            await Assert.ThrowsAsync <TeacherContactValidationException>(() =>
                                                                         addTeacherContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertTeacherContactAsync(invalidTeacherContact),
                                          Times.Once);

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