private async ValueTask <StudentContact> TryCatch( ReturningStudentContactFunction returningStudentContactFunction) { try { return(await returningStudentContactFunction()); } catch (NullStudentContactException nullStudentContactException) { throw CreateAndLogValidationException(nullStudentContactException); } catch (InvalidStudentContactException invalidStudentContactException) { throw CreateAndLogValidationException(invalidStudentContactException); } catch (NotFoundStudentContactException notFoundStudentContactException) { throw CreateAndLogValidationException(notFoundStudentContactException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentContactException = new AlreadyExistsStudentContactException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentContactException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidStudentContactReferenceException = new InvalidStudentContactReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidStudentContactReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedStudentContactException = new LockedStudentContactException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedStudentContactException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentContactServiceException = new FailedStudentContactServiceException(exception); throw CreateAndLogServiceException(failedStudentContactServiceException); } }
public async void ShouldThrowValidationExceptionOnAddWhenReferneceExceptionAndLogItAsync() { // given StudentContact randomStudentContact = CreateRandomStudentContact(); StudentContact invalidStudentContact = randomStudentContact; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var foreignKeyConstraintConflictException = new ForeignKeyConstraintConflictException(exceptionMessage); var invalidStudentContactReferenceException = new InvalidStudentContactReferenceException(foreignKeyConstraintConflictException); var expectedStudentContactValidationException = new StudentContactValidationException(invalidStudentContactReferenceException); this.storageBrokerMock.Setup(broker => broker.InsertStudentContactAsync(invalidStudentContact)) .ThrowsAsync(foreignKeyConstraintConflictException); // when ValueTask <StudentContact> addStudentContactTask = this.studentContactService.AddStudentContactAsync(invalidStudentContact); // then await Assert.ThrowsAsync <StudentContactValidationException>(() => addStudentContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentContactValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentContactAsync(invalidStudentContact), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }