Skip to content

Commit

Permalink
fix: This event loop is already running (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmacias95 authored Feb 23, 2023
1 parent b3ee698 commit 250a235
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
6 changes: 5 additions & 1 deletion examples/utils/dictpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
_PARSED_PATH_CACHE = {}


class DictPathException(Exception):
"""Represents exceptions raised by the dictpath module."""


def iterate(data, path):
"""Generator that returns values in data matching the given JsonPath."""

Expand Down Expand Up @@ -57,5 +61,5 @@ def get(data, path, default=None):
if not result:
return default
if len(result) > 1:
raise Exception(f'JsonPath {path} returning more than one value')
raise DictPathException(f'JsonPath {path} returning more than one value')
return result[0]
6 changes: 3 additions & 3 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,6 @@ valid-metaclass-classmethod-first-arg=mcs

# Exceptions that will emit a warning when being caught. Defaults to
# "Exception"
overgeneral-exceptions=StandardError,
Exception,
BaseException
overgeneral-exceptions=builtins.StandardError,
builtins.Exception,
builtins.BaseException
26 changes: 23 additions & 3 deletions tests/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"""Tests features defined in vt/iterator.py."""

from collections import abc

import json
import pytest

from vt import Client
import vt


def new_client(httpserver):
return Client('dummy_api_key',
return vt.Client('dummy_api_key',
host='http://' + httpserver.host + ':' + str(httpserver.port))


Expand Down Expand Up @@ -161,3 +161,23 @@ async def test_anext(httpserver, iterator_response): # pylint: disable=unused-a
# trying to iterate over next element must not work
async for obj in it:
pytest.fail('Iteration should already be finished')


def test_apierror_iterator(httpserver):
"""Tests errors are handled gracefully when iterating over a collection."""
expected_error = {
'data': {'error': 'InvalidArgumentError', 'message': 'Invalid args'}}

httpserver.expect_request('/api/v3/dummy_collection/foo').respond_with_json(
expected_error, status=400)

result = []
with new_client(httpserver) as client:
it = client.iterator('/dummy_collection/foo', limit=10, batch_size=3)

with pytest.raises(vt.APIError) as e:
for i in it:
result.append(i)

assert e.value.code == 'ClientError'
assert json.loads(e.value.message)['data'] == expected_error['data']
3 changes: 0 additions & 3 deletions vt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,6 @@ def __enter__(self):
def __exit__(self, item_type, value, traceback):
self.close()

def __del__(self):
self.close()

def _extract_data_from_json(self, json_response):
if not 'data' in json_response:
raise ValueError('response does not returns a data field')
Expand Down

0 comments on commit 250a235

Please sign in to comment.