diff --git a/examples/utils/dictpath.py b/examples/utils/dictpath.py index 756f64c..2da648c 100644 --- a/examples/utils/dictpath.py +++ b/examples/utils/dictpath.py @@ -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.""" @@ -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] diff --git a/pylintrc b/pylintrc index be7798e..140a876 100644 --- a/pylintrc +++ b/pylintrc @@ -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 diff --git a/tests/test_iterator.py b/tests/test_iterator.py index 65c0ade..0ac194a 100644 --- a/tests/test_iterator.py +++ b/tests/test_iterator.py @@ -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)) @@ -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'] diff --git a/vt/client.py b/vt/client.py index b3d6ab3..3dd6f3d 100644 --- a/vt/client.py +++ b/vt/client.py @@ -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')