private async ValueTask <UserContact> TryCatch(ReturningUserContactFunction returningUserContactFunction) { try { return(await returningUserContactFunction()); } catch (NullUserContactException nullUserContactException) { throw CreateAndLogValidationException(nullUserContactException); } catch (InvalidUserContactException invalidUserContactException) { throw CreateAndLogValidationException(invalidUserContactException); } catch (SqlException sqlException) { var failedUserContactStorageException = new FailedUserContactStorageException(sqlException); throw CreateAndLogCriticalDependencyException(failedUserContactStorageException); } catch (NotFoundUserContactException notFoundUserContactException) { throw CreateAndLogValidationException(notFoundUserContactException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsUserContactException = new AlreadyExistsUserContactException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsUserContactException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidUserContactReferenceException = new InvalidUserContactReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidUserContactReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedUserContactException = new LockedUserContactException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedUserContactException); } catch (DbUpdateException dbUpdateException) { var failedUserContactStorageException = new FailedUserContactStorageException(dbUpdateException); throw CreateAndLogDependencyException(failedUserContactStorageException); } catch (Exception exception) { throw CreateAndLogServiceException(exception); } }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given var randomContactId = Guid.NewGuid(); var randomUserId = Guid.NewGuid(); Guid someContactId = randomContactId; Guid someUserId = randomUserId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedContactException = new LockedUserContactException(databaseUpdateConcurrencyException); var expectedUserContactException = new UserContactDependencyException(lockedContactException); this.storageBrokerMock.Setup(broker => broker.SelectUserContactByIdAsync(someUserId, someContactId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <UserContact> removeUserContactTask = this.userContactService.RemoveUserContactByIdAsync(someUserId, someContactId); // then await Assert.ThrowsAsync <UserContactDependencyException>(() => removeUserContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedUserContactException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectUserContactByIdAsync(someUserId, someContactId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteUserContactAsync(It.IsAny <UserContact>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }