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); } }
public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given ExamAttachment randomExamAttachment = CreateRandomExamAttachment(); ExamAttachment invalidExamAttachment = randomExamAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidExamAttachmentReferenceException = new InvalidExamAttachmentReferenceException(foreignKeyConstraintConflictException); var expectedExamAttachmentValidationException = new ExamAttachmentValidationException(invalidExamAttachmentReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertExamAttachmentAsync(invalidExamAttachment)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <ExamAttachment> addExamAttachmentTask = this.examAttachmentService.AddExamAttachmentAsync(invalidExamAttachment); // 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(invalidExamAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }