private async ValueTask <TeacherContact> TryCatch( ReturningTeacherContactFunction returningTeacherContactFunction) { try { return(await returningTeacherContactFunction()); } catch (NullTeacherContactException nullTeacherContactException) { throw CreateAndLogValidationException(nullTeacherContactException); } catch (InvalidTeacherContactException invalidTeacherContactException) { throw CreateAndLogValidationException(invalidTeacherContactException); } catch (NotFoundTeacherContactException notFoundTeacherContactException) { throw CreateAndLogValidationException(notFoundTeacherContactException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsTeacherContactException = new AlreadyExistsTeacherContactException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsTeacherContactException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidTeacherContactReferenceException = new InvalidTeacherContactReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidTeacherContactReferenceException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedTeacherContactException = new LockedTeacherContactException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedTeacherContactException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedTeacherContactServiceException = new FailedTeacherContactServiceException(exception); throw CreateAndLogServiceException(failedTeacherContactServiceException); } }
public async void ShouldThrowValidationExceptionOnAddWhenTeacherContactAlreadyExistsAndLogItAsync() { // given TeacherContact randomTeacherContact = CreateRandomTeacherContact(); TeacherContact alreadyExistsTeacherContact = randomTeacherContact; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsTeacherContactException = new AlreadyExistsTeacherContactException(duplicateKeyException); var expectedTeacherContactValidationException = new TeacherContactValidationException(alreadyExistsTeacherContactException); this.storageBrokerMock.Setup(broker => broker.InsertTeacherContactAsync(alreadyExistsTeacherContact)) .ThrowsAsync(duplicateKeyException); // when ValueTask <TeacherContact> addTeacherContactTask = this.teacherContactService.AddTeacherContactAsync(alreadyExistsTeacherContact); // then await Assert.ThrowsAsync <TeacherContactValidationException>(() => addTeacherContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherContactValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertTeacherContactAsync(alreadyExistsTeacherContact), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }