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 ShouldThrowValidationExceptionOnAddWhenStudentAttachmentAlreadyExistsAndLogItAsync() { // given StudentAttachment randomStudentAttachment = CreateRandomStudentAttachment(); StudentAttachment alreadyExistsStudentAttachment = randomStudentAttachment; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsStudentAttachmentException = new AlreadyExistsStudentAttachmentException(duplicateKeyException); var expectedStudentAttachmentValidationException = new StudentAttachmentValidationException(alreadyExistsStudentAttachmentException); this.storageBrokerMock.Setup(broker => broker.InsertStudentAttachmentAsync(alreadyExistsStudentAttachment)) .ThrowsAsync(duplicateKeyException); // when ValueTask <StudentAttachment> addStudentAttachmentTask = this.studentAttachmentService.AddStudentAttachmentAsync(alreadyExistsStudentAttachment); // 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(alreadyExistsStudentAttachment), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }