private async ValueTask <StudentAttachment> TryCatch( ReturningStudentAttachmentFunction returningStudentAttachmentFunction) { try { return(await returningStudentAttachmentFunction()); } catch (NullStudentAttachmentException nullStudentAttachmentInputException) { throw CreateAndLogValidationException(nullStudentAttachmentInputException); } catch (InvalidStudentAttachmentException invalidStudentAttachmentInputException) { throw CreateAndLogValidationException(invalidStudentAttachmentInputException); } catch (NotFoundStudentAttachmentException notFoundStudentAttachmentException) { throw CreateAndLogValidationException(notFoundStudentAttachmentException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentAttachmentException = new AlreadyExistsStudentAttachmentException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentAttachmentException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidStudentAttachmentReferenceException = new InvalidStudentAttachmentReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidStudentAttachmentReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedAttachmentException = new LockedStudentAttachmentException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedAttachmentException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentAttachmentServiceException = new FailedStudentAttachmentServiceException(exception); throw CreateAndLogServiceException(failedStudentAttachmentServiceException); } }
public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment(); StudentAttachment invalidStudentAttachment = randomStudentAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidStudentAttachmentReferenceException = new InvalidStudentAttachmentReferenceException(foreignKeyConstraintConflictException); var expectedStudentAttachmentValidationException = new StudentAttachmentValidationException(invalidStudentAttachmentReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertStudentAttachmentAsync(invalidStudentAttachment)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <StudentAttachment> addStudentAttachmentTask = this.studentAttachmentService.AddStudentAttachmentAsync(invalidStudentAttachment); // then await Assert.ThrowsAsync <StudentAttachmentValidationException>(() => addStudentAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentAttachmentAsync(invalidStudentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }