private async ValueTask <CourseAttachment> TryCatch(
            ReturningCourseAttachmentFunction returningCourseAttachmentFunction)
        {
            try
            {
                return(await returningCourseAttachmentFunction());
            }
            catch (NullCourseAttachmentException nullCourseAttachmentException)
            {
                throw CreateAndLogValidationException(nullCourseAttachmentException);
            }
            catch (InvalidCourseAttachmentException invalidCourseAttachmentException)
            {
                throw CreateAndLogValidationException(invalidCourseAttachmentException);
            }
            catch (NotFoundCourseAttachmentException notFoundCourseAttachmentException)
            {
                throw CreateAndLogValidationException(notFoundCourseAttachmentException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsCourseAttachmentException =
                    new AlreadyExistsCourseAttachmentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsCourseAttachmentException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidCourseAttachmentReferenceException =
                    new InvalidCourseAttachmentReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidCourseAttachmentReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedCourseAttachmentException =
                    new LockedCourseAttachmentException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedCourseAttachmentException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedCourseAttachmentServiceException =
                    new FailedCourseAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedCourseAttachmentServiceException);
            }
        }
Exemplo n.º 2
0
        public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync()
        {
            // given
            CourseAttachment randomCourseAttachment   = CreateRandomCourseAttachment();
            CourseAttachment someCourseAttachment     = randomCourseAttachment;
            string           randomMessage            = GetRandomMessage();
            string           exceptionMessage         = randomMessage;
            var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage);

            var invalidCourseAttachmentReferenceException =
                new InvalidCourseAttachmentReferenceException(foreignKeyConstraintConflictException);

            var expectedCourseAttachmentValidationException =
                new CourseAttachmentValidationException(invalidCourseAttachmentReferenceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCourseAttachmentAsync(someCourseAttachment))
            .ThrowsAsync(foreignKeyConstraintConflictException);

            // when
            ValueTask <CourseAttachment> addCourseAttachmentTask =
                this.courseAttachmentService.AddCourseAttachmentAsync(someCourseAttachment);

            // then
            await Assert.ThrowsAsync <CourseAttachmentValidationException>(() =>
                                                                           addCourseAttachmentTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCourseAttachmentAsync(It.IsAny <CourseAttachment>()),
                                          Times.Once);

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

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