errors: Start module.

This commit is contained in:
Brett Smith 2017-05-12 08:12:21 -04:00
parent 1927a18120
commit 4cbd890992
5 changed files with 23 additions and 33 deletions

5
oxrlib/errors.py Normal file
View file

@ -0,0 +1,5 @@
class LoaderError(Exception): pass
class LoaderNoDataError(LoaderError): pass
class LoaderBadRequestError(LoaderError): pass
class LoaderSourceError(LoaderError): pass
class NoLoadersError(Exception): pass

View file

@ -4,25 +4,7 @@ import io
import urllib.request
import urllib.parse
class LoaderError(Exception):
pass
class LoaderNoDataError(LoaderError):
pass
class LoaderBadRequestError(LoaderError):
pass
class LoaderSourceError(LoaderError):
pass
class NoLoadersError(Exception):
pass
from . import errors
class FileCache:
def __init__(self, dir_path, filename_pattern):
@ -34,7 +16,7 @@ class FileCache:
try:
return path.open()
except FileNotFoundError as error:
raise LoaderNoDataError(path) from error
raise errors.LoaderNoDataError(path) from error
class OXRAPIRequest:
@ -67,11 +49,11 @@ class OXRAPIRequest:
if 200 <= status_code < 203:
return response_body
elif status_code == 404 or status_code == 410:
exc_class = LoaderNoDataError
exc_class = errors.LoaderNoDataError
elif status_code >= 500:
exc_class = LoaderSourceError
exc_class = errors.LoaderSourceError
else:
exc_class = LoaderBadRequestError
exc_class = errors.LoaderBadRequestError
with response_body:
raise exc_class(url, response_body.read(64 * 1024))
@ -97,13 +79,13 @@ class LoaderChain:
for loader in self.loaders:
try:
response = getattr(loader, orig_func.__name__)(*args, **kwargs)
except LoaderError as this_error:
except errors.LoaderError as this_error:
error = this_error
else:
self.used_loader = loader
return response
else:
raise NoLoadersError() if error is None else error
raise errors.NoLoadersError() if error is None else error
return load_wrapper
@_wrap_load_method

View file

@ -4,6 +4,7 @@ import pathlib
import pytest
from . import relpath
import oxrlib.errors
import oxrlib.loaders
CACHE_PATH = relpath('filecache')
@ -29,7 +30,7 @@ def test_cache_success(dummycache, date, base):
def test_cache_not_found(dummycache, date, base):
try:
cache_file = dummycache.historical(date, base)
except oxrlib.loaders.LoaderNoDataError:
except oxrlib.errors.LoaderNoDataError:
pass
else:
cache_file.close()

View file

@ -2,12 +2,13 @@ import io
import pytest
import oxrlib.errors
import oxrlib.loaders
from . import any_date
SUCCESS_S = '"success"\n'
ERROR = oxrlib.loaders.LoaderNoDataError("test")
ERROR = oxrlib.errors.LoaderNoDataError("test")
class FakeLoader:
def __init__(self, result):
@ -43,7 +44,7 @@ def error_loader():
def test_no_loaders(lchain, any_date):
try:
lchain.historical(any_date, 'USD')
except oxrlib.loaders.NoLoadersError:
except oxrlib.errors.NoLoadersError:
pass
else:
assert False, "expected NoLoadersError not raised"

View file

@ -7,6 +7,7 @@ import string
import urllib.parse
import pytest
import oxrlib.errors
import oxrlib.loaders
from . import any_date
@ -82,11 +83,11 @@ def test_success(api_client, any_date, base):
assert response.read() == (json.dumps(body) + "\n")
@pytest.mark.parametrize('status_code,expect_exctype', [
(400, oxrlib.loaders.LoaderBadRequestError),
(403, oxrlib.loaders.LoaderBadRequestError),
(404, oxrlib.loaders.LoaderNoDataError),
(410, oxrlib.loaders.LoaderNoDataError),
(500, oxrlib.loaders.LoaderSourceError),
(400, oxrlib.errors.LoaderBadRequestError),
(403, oxrlib.errors.LoaderBadRequestError),
(404, oxrlib.errors.LoaderNoDataError),
(410, oxrlib.errors.LoaderNoDataError),
(500, oxrlib.errors.LoaderSourceError),
])
def test_failure(api_client, any_date, status_code, expect_exctype):
opener = FakeOpener(FakeResponse(status_code))