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 Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given var randomContactId = Guid.NewGuid(); var randomTeacherId = Guid.NewGuid(); Guid someContactId = randomContactId; Guid someTeacherId = randomTeacherId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedContactException = new LockedTeacherContactException(databaseUpdateConcurrencyException); var expectedTeacherContactException = new TeacherContactDependencyException(lockedContactException); this.storageBrokerMock.Setup(broker => broker.SelectTeacherContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <TeacherContact> removeTeacherContactTask = this.teacherContactService.RemoveTeacherContactByIdAsync(someTeacherId, someContactId); // then await Assert.ThrowsAsync <TeacherContactDependencyException>(() => removeTeacherContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherContactException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTeacherContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteTeacherContactAsync(It.IsAny <TeacherContact>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }