From 2214efffcfc307a2bf62b4fdda6b40b5f560cbda Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 26 Dec 2020 16:13:12 -0800 Subject: [PATCH] Add exception and handling for when configuration directory is actually a file --- jrnl/cli.py | 5 +++++ jrnl/config.py | 9 ++++++++- jrnl/exception.py | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/jrnl/cli.py b/jrnl/cli.py index e010f38e..64a728ed 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -7,6 +7,7 @@ import sys from .jrnl import run from .args import parse_args +from .exception import ConfigDirectoryPathIsFileException def configure_logger(debug=False): @@ -33,5 +34,9 @@ def cli(manual_args=None): return run(args) + except ConfigDirectoryPathIsFileException as e: + print(e, file=sys.stderr) + return 1 + except KeyboardInterrupt: return 1 diff --git a/jrnl/config.py b/jrnl/config.py index bcf7e49a..c7b67ac7 100644 --- a/jrnl/config.py +++ b/jrnl/config.py @@ -7,6 +7,7 @@ import yaml import xdg.BaseDirectory from . import __version__ +from .exception import ConfigDirectoryPathIsFileException from .color import ERROR_COLOR from .color import RESET_COLOR from .output import list_journals @@ -33,7 +34,13 @@ def get_config_path(): except FileExistsError: # .TODO raise a custom jrnl exception # this is when XDG tries to create a directory with the same name as a file that exists - raise + + 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." + ) 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 f1a509f5..7653c19d 100644 --- a/jrnl/exception.py +++ b/jrnl/exception.py @@ -10,3 +10,8 @@ class UpgradeValidationException(Exception): """Raised when the contents of an upgraded journal do not match the old journal""" pass + + +class ConfigDirectoryPathIsFileException(Exception): + + pass