private async ValueTask <Classroom> TryCatch(ReturningClassroomFunction returningClassroomFunction)
        {
            try
            {
                return(await returningClassroomFunction());
            }
            catch (NullClassroomException nullClassroomException)
            {
                throw CreateAndLogValidationException(nullClassroomException);
            }
            catch (InvalidClassroomException invalidClassroomInputException)
            {
                throw CreateAndLogValidationException(invalidClassroomInputException);
            }
            catch (NotFoundClassroomException notFoundClassroomException)
            {
                throw CreateAndLogValidationException(notFoundClassroomException);
            }
            catch (SqlException sqlException)
            {
                var failedClassroomStorageException =
                    new FailedClassroomStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedClassroomStorageException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsClassroomException =
                    new AlreadyExistsClassroomException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsClassroomException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedClassroomException = new LockedClassroomException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyValidationException(lockedClassroomException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                var failedClassroomStorageException =
                    new FailedClassroomStorageException(dbUpdateException);

                throw CreateAndLogDependencyException(failedClassroomStorageException);
            }
            catch (Exception exception)
            {
                var failedClassroomServiceException =
                    new FailedClassroomServiceException(exception);

                throw CreateAndLogServiceException(failedClassroomServiceException);
            }
        }
Beispiel #2
0
        public async void ShouldThrowValidationExceptionOnCreateWhenClassroomAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime               = GetRandomDateTime();
            Classroom      randomClassroom        = CreateRandomClassroom(dateTime);
            Classroom      alreadyExistsClassroom = randomClassroom;

            alreadyExistsClassroom.UpdatedBy = alreadyExistsClassroom.CreatedBy;
            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsClassroomException =
                new AlreadyExistsClassroomException(duplicateKeyException);

            var expectedClassroomValidationException =
                new ClassroomValidationException(alreadyExistsClassroomException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertClassroomAsync(alreadyExistsClassroom))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Classroom> createClassroomTask =
                this.classroomService.CreateClassroomAsync(alreadyExistsClassroom);

            // then
            await Assert.ThrowsAsync <ClassroomValidationException>(() =>
                                                                    createClassroomTask.AsTask());

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

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

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

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