private async ValueTask <ExamAttachment> TryCatch(
            ReturningExamAttachmentFunction returningExamAttachmentFunction)
        {
            try
            {
                return(await returningExamAttachmentFunction());
            }

            catch (NullExamAttachmentException nullExamAttachmentException)
            {
                throw CreateAndLogValidationException(nullExamAttachmentException);
            }
            catch (InvalidExamAttachmentException invalidExamAttachmentInputException)
            {
                throw CreateAndLogValidationException(invalidExamAttachmentInputException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (NotFoundExamAttachmentException notFoundExamAttachmentException)
            {
                throw CreateAndLogValidationException(notFoundExamAttachmentException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsExamAttachmentException =
                    new AlreadyExistsExamAttachmentException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsExamAttachmentException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidExamAttachmentReferenceException =
                    new InvalidExamAttachmentReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidExamAttachmentReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedExamAttachmentException =
                    new LockedExamAttachmentException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedExamAttachmentException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedExamAttachmentServiceException =
                    new FailedExamAttachmentServiceException(exception);

                throw CreateAndLogServiceException(failedExamAttachmentServiceException);
            }
        }
Exemple #2
0
        public async void ShouldThrowValidationExceptionOnAddWhenExamAttachmentAlreadyExistsAndLogItAsync()
        {
            // given
            ExamAttachment randomExamAttachment        = CreateRandomExamAttachment();
            ExamAttachment alreadyExistsExamAttachment = randomExamAttachment;
            string         randomMessage         = GetRandomMessage();
            string         exceptionMessage      = randomMessage;
            var            duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsExamAttachmentException =
                new AlreadyExistsExamAttachmentException(duplicateKeyException);

            var expectedExamAttachmentValidationException =
                new ExamAttachmentValidationException(alreadyExistsExamAttachmentException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertExamAttachmentAsync(alreadyExistsExamAttachment))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <ExamAttachment> addExamAttachmentTask =
                this.examAttachmentService.AddExamAttachmentAsync(alreadyExistsExamAttachment);

            // then
            await Assert.ThrowsAsync <ExamAttachmentValidationException>(() =>
                                                                         addExamAttachmentTask.AsTask());

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

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

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