private async ValueTask <Exam> TryCatch(ReturningExamFunction returningExamFunction)
        {
            try
            {
                return(await returningExamFunction());
            }
            catch (NullExamException nullExamException)
            {
                throw CreateAndLogValidationException(nullExamException);
            }
            catch (InvalidExamException invalidExamInputException)
            {
                throw CreateAndLogValidationException(invalidExamInputException);
            }
            catch (NotFoundExamException nullExamException)
            {
                throw CreateAndLogValidationException(nullExamException);
            }
            catch (SqlException sqlException)
            {
                var failedExamStorageException =
                    new FailedExamStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedExamStorageException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsExamException =
                    new AlreadyExistsExamException(duplicateKeyException);

                throw CreateAndLogDependencyValidationException(alreadyExistsExamException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedExamException = new LockedExamException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyValidationException(lockedExamException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                var failedExamStorageException =
                    new FailedExamStorageException(dbUpdateException);

                throw CreateAndLogDependencyException(failedExamStorageException);
            }
            catch (Exception exception)
            {
                var failedExamServiceException =
                    new FailedExamServiceException(exception);

                throw CreateAndLogServiceException(failedExamServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceErrorOccursAndLogItAsync()
        {
            // given
            Exam someExam         = CreateRandomExam();
            var  serviceException = new Exception();

            var failedExamServiceException =
                new FailedExamServiceException(serviceException);

            var expectedExamServiceException =
                new ExamServiceException(failedExamServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(serviceException);

            // when
            ValueTask <Exam> modifyExamTask =
                this.examService.ModifyExamAsync(someExam);

            // then
            await Assert.ThrowsAsync <ExamServiceException>(() =>
                                                            modifyExamTask.AsTask());

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamByIdAsync(someExam.Id),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <Exam> TryCatch(ReturningQueryableExamFunction returningQueryableExamFunction)
        {
            try
            {
                return(returningQueryableExamFunction());
            }
            catch (SqlException sqlException)
            {
                var failedExamStorageException =
                    new FailedExamStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedExamStorageException);
            }
            catch (Exception exception)
            {
                var failedExamServiceException =
                    new FailedExamServiceException(exception);

                throw CreateAndLogServiceException(failedExamServiceException);
            }
        }
Esempio n. 4
0
        public async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync()
        {
            // given
            Guid someExamId       = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedExamServiceException =
                new FailedExamServiceException(serviceException);

            var expectedExamServiceException =
                new ExamServiceException(failedExamServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectExamByIdAsync(someExamId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Exam> retrieveExamByIdTask =
                this.examService.RetrieveExamByIdAsync(someExamId);

            // then
            await Assert.ThrowsAsync <ExamServiceException>(() =>
                                                            retrieveExamByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectExamByIdAsync(someExamId),
                                          Times.Once);

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedExamServiceException =
                new FailedExamServiceException(serviceException);

            var expectedExamServiceException =
                new ExamServiceException(failedExamServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllExams())
            .Throws(serviceException);

            // when
            Action retrieveAllExamsAction = () =>
                                            this.examService.RetrieveAllExams();

            // then
            Assert.Throws <ExamServiceException>(
                retrieveAllExamsAction);

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

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

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