From b7fcad7cc1770cada2983ac2c552a37304fb767a Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 13 Dec 2017 14:07:52 -0800 Subject: [PATCH] Start exception hierarchy; rename DBNotFound to DatabaseNotFound. --- timesearch/__init__.py | 5 ++--- timesearch/exceptions.py | 22 +++++++++++++++++++--- timesearch/tsdb.py | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/timesearch/__init__.py b/timesearch/__init__.py index 5ebe981..39b4be0 100644 --- a/timesearch/__init__.py +++ b/timesearch/__init__.py @@ -423,10 +423,9 @@ def main(argv): args = parser.parse_args(argv) try: args.func(args) - except exceptions.DBNotFound as e: - message = '"%s" is not an existing database.' + except exceptions.DatabaseNotFound as e: + message = str(e) message += '\nHave you used any of the other utilities to collect data?' - message = message % e.path.absolute_path print(message) return 1 diff --git a/timesearch/exceptions.py b/timesearch/exceptions.py index b2142d8..c9f03c4 100644 --- a/timesearch/exceptions.py +++ b/timesearch/exceptions.py @@ -1,3 +1,19 @@ -class DBNotFound(FileNotFoundError): - def __init__(self, path): - self.path = path +class TimesearchException(Exception): + ''' + Base type for all of the Timesearch exceptions. + Subtypes should have a class attribute `error_message`. The error message + may contain {format} strings which will be formatted using the + Exception's constructor arguments. + ''' + error_message = '' + def __init__(self, *args, **kwargs): + self.given_args = args + self.given_kwargs = kwargs + self.error_message = self.error_message.format(*args, **kwargs) + self.args = (self.error_message, args, kwargs) + + def __str__(self): + return self.error_message + +class DatabaseNotFound(TimesearchException, FileNotFoundError): + error_message = 'Database file not found: "{}"' diff --git a/timesearch/tsdb.py b/timesearch/tsdb.py index d400fe0..2b9d052 100644 --- a/timesearch/tsdb.py +++ b/timesearch/tsdb.py @@ -124,7 +124,7 @@ class TSDB: self.filepath = pathclass.Path(filepath) if not self.filepath.is_file: if not do_create: - raise exceptions.DBNotFound(self.filepath) + raise exceptions.DatabaseNotFound(self.filepath) print('New database', self.filepath.relative_path) os.makedirs(self.filepath.parent.absolute_path, exist_ok=True)