mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 04:58:32 +02:00
Use more generic JrnlError with messaging switchboard
This commit is contained in:
parent
75269ede85
commit
7ac104a60d
4 changed files with 37 additions and 10 deletions
|
@ -7,7 +7,7 @@ import sys
|
||||||
|
|
||||||
from .jrnl import run
|
from .jrnl import run
|
||||||
from .args import parse_args
|
from .args import parse_args
|
||||||
from .exception import ConfigDirectoryPathIsFileException
|
from .exception import JrnlError
|
||||||
|
|
||||||
|
|
||||||
def configure_logger(debug=False):
|
def configure_logger(debug=False):
|
||||||
|
@ -34,7 +34,7 @@ def cli(manual_args=None):
|
||||||
|
|
||||||
return run(args)
|
return run(args)
|
||||||
|
|
||||||
except ConfigDirectoryPathIsFileException as e:
|
except JrnlError as e:
|
||||||
print(e, file=sys.stderr)
|
print(e, file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import yaml
|
||||||
import xdg.BaseDirectory
|
import xdg.BaseDirectory
|
||||||
|
|
||||||
from . import __version__
|
from . import __version__
|
||||||
from .exception import ConfigDirectoryPathIsFileException
|
from .exception import JrnlError
|
||||||
from .color import ERROR_COLOR
|
from .color import ERROR_COLOR
|
||||||
from .color import RESET_COLOR
|
from .color import RESET_COLOR
|
||||||
from .output import list_journals
|
from .output import list_journals
|
||||||
|
@ -32,13 +32,12 @@ def get_config_path():
|
||||||
try:
|
try:
|
||||||
config_directory_path = xdg.BaseDirectory.save_config_path(XDG_RESOURCE)
|
config_directory_path = xdg.BaseDirectory.save_config_path(XDG_RESOURCE)
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
raise ConfigDirectoryPathIsFileException(
|
raise JrnlError(
|
||||||
"The path to your jrnl configuration directory is a file, not a directory:\n"
|
"ConfigDirectoryIsFile",
|
||||||
+ os.path.join(xdg.BaseDirectory.xdg_config_home, XDG_RESOURCE)
|
config_directory_path=os.path.join(
|
||||||
+ "\n"
|
xdg.BaseDirectory.xdg_config_home, XDG_RESOURCE
|
||||||
+ "Removing this file will allow jrnl to save its configuration."
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
return os.path.join(
|
return os.path.join(
|
||||||
config_directory_path or os.path.expanduser("~"), DEFAULT_CONFIG_NAME
|
config_directory_path or os.path.expanduser("~"), DEFAULT_CONFIG_NAME
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,6 +12,21 @@ class UpgradeValidationException(Exception):
|
||||||
pass
|
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
|
pass
|
||||||
|
|
13
tests/test_exception.py
Normal file
13
tests/test_exception.py
Normal file
|
@ -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."
|
||||||
|
)
|
Loading…
Add table
Reference in a new issue