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

View file

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

View file

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

View file

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