From 744479013d7a8a7015a2ae6a248f909bccce4694 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Tue, 9 May 2017 10:35:20 -0400 Subject: [PATCH] loaders: Introduce common exceptions. --- oxrlib/loaders.py | 22 ++++++++++++++++++++-- tests/test_loaders.py | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/oxrlib/loaders.py b/oxrlib/loaders.py index 95020c9..3e4d22b 100644 --- a/oxrlib/loaders.py +++ b/oxrlib/loaders.py @@ -1,3 +1,19 @@ +class LoaderError(Exception): + pass + + +class LoaderNoDataError(LoaderError): + pass + + +class LoaderBadRequestError(LoaderError): + pass + + +class LoaderSourceError(LoaderError): + pass + + class FileCache: def __init__(self, dir_path, filename_pattern): self.dir_path = dir_path @@ -5,5 +21,7 @@ class FileCache: def historical(self, date, base): path = self.dir_path / self.pattern.format(date=date.isoformat(), base=base) - return path.open() - + try: + return path.open() + except FileNotFoundError as error: + raise LoaderNoDataError(path) from error diff --git a/tests/test_loaders.py b/tests/test_loaders.py index 841b56a..76f2096 100644 --- a/tests/test_loaders.py +++ b/tests/test_loaders.py @@ -29,7 +29,7 @@ def test_cache_success(dummycache, date, base): def test_cache_not_found(dummycache, date, base): try: cache_file = dummycache.historical(date, base) - except FileNotFoundError: + except oxrlib.loaders.LoaderNoDataError: pass else: cache_file.close()