From 7ac104a60ddaab8b33fb9e064fe6c6868abfa07a Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 2 Jan 2021 14:09:32 -0800 Subject: [PATCH] Use more generic JrnlError with messaging switchboard --- jrnl/cli.py | 4 ++-- jrnl/config.py | 13 ++++++------- jrnl/exception.py | 17 ++++++++++++++++- tests/test_exception.py | 13 +++++++++++++ 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 tests/test_exception.py diff --git a/jrnl/cli.py b/jrnl/cli.py index 64a728ed..67109080 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -7,7 +7,7 @@ import sys from .jrnl import run from .args import parse_args -from .exception import ConfigDirectoryPathIsFileException +from .exception import JrnlError def configure_logger(debug=False): @@ -34,7 +34,7 @@ def cli(manual_args=None): return run(args) - except ConfigDirectoryPathIsFileException as e: + except JrnlError as e: print(e, file=sys.stderr) return 1 diff --git a/jrnl/config.py b/jrnl/config.py index d2e671ae..9e57de3d 100644 --- a/jrnl/config.py +++ b/jrnl/config.py @@ -7,7 +7,7 @@ import yaml import xdg.BaseDirectory from . import __version__ -from .exception import ConfigDirectoryPathIsFileException +from .exception import JrnlError from .color import ERROR_COLOR from .color import RESET_COLOR from .output import list_journals @@ -32,13 +32,12 @@ def get_config_path(): try: config_directory_path = xdg.BaseDirectory.save_config_path(XDG_RESOURCE) except FileExistsError: - raise ConfigDirectoryPathIsFileException( - "The path to your jrnl configuration directory is a file, not a directory:\n" - + os.path.join(xdg.BaseDirectory.xdg_config_home, XDG_RESOURCE) - + "\n" - + "Removing this file will allow jrnl to save its configuration." + raise JrnlError( + "ConfigDirectoryIsFile", + config_directory_path=os.path.join( + xdg.BaseDirectory.xdg_config_home, XDG_RESOURCE + ), ) - return os.path.join( config_directory_path or os.path.expanduser("~"), DEFAULT_CONFIG_NAME ) diff --git a/jrnl/exception.py b/jrnl/exception.py index 7653c19d..0ffc27fb 100644 --- a/jrnl/exception.py +++ b/jrnl/exception.py @@ -12,6 +12,21 @@ class UpgradeValidationException(Exception): pass -class ConfigDirectoryPathIsFileException(Exception): +class JrnlError(Exception): + """Common exceptions raised by jrnl. """ + + def __init__(self, error_type, **kwargs): + self.error_type = error_type + self.message = self.get_error_message(**kwargs) + + def get_error_message(self, **kwargs): + + error_messages = { + "ConfigDirectoryIsFile": "The path to your jrnl configuration directory is a file, not a directory:\n" + + "{config_directory_path}\n" + + "Removing this file will allow jrnl to save its configuration." + } + + return error_messages[self.error_type].format(**kwargs) pass diff --git a/tests/test_exception.py b/tests/test_exception.py new file mode 100644 index 00000000..a369ac08 --- /dev/null +++ b/tests/test_exception.py @@ -0,0 +1,13 @@ +from jrnl.exception import JrnlError + + +def test_config_directory_exception_message(): + ex = JrnlError( + "ConfigDirectoryIsFile", config_directory_path="/config/directory/path" + ) + + assert ex.message == ( + "The path to your jrnl configuration directory is a file, not a directory:\n" + + "/config/directory/path\n" + + "Removing this file will allow jrnl to save its configuration." + )