move some exceptions and errors to the new exception handling

This commit is contained in:
Jonathan Wren 2022-02-19 16:22:22 -08:00
parent 2d8ffd1174
commit b907064639
3 changed files with 33 additions and 50 deletions

View file

@ -9,12 +9,6 @@ class UserAbort(Exception):
pass pass
class UpgradeValidationException(Exception):
"""Raised when the contents of an upgraded journal do not match the old journal"""
pass
class JrnlExceptionMessage(Enum): class JrnlExceptionMessage(Enum):
ConfigDirectoryIsFile = """ ConfigDirectoryIsFile = """
The path to your jrnl configuration directory is a file, not a directory: The path to your jrnl configuration directory is a file, not a directory:
@ -51,12 +45,24 @@ class JrnlExceptionMessage(Enum):
editor: '{editor_key}' editor: '{editor_key}'
""" """
JournalFailedUpgrade = """
The following journal{s} failed to upgrade:
{failed_journals}
Please tell us about this problem at the following URL:
https://github.com/jrnl-org/jrnl/issues/new?title=JournalFailedUpgrade
"""
UpgradeAborted = """
jrnl was NOT upgraded
"""
SomeTest = """ SomeTest = """
Some error or something Some error or something
This is a thing to test with this message or whatever and maybe it just This is a thing to test with this message or whatever and maybe it just
keeps going forever because it's super long for no apparent reason keeps going forever because it's super long for no apparent reason
""" """
class JrnlException(Exception): class JrnlException(Exception):

View file

@ -14,7 +14,6 @@ from .config import get_default_journal_path
from .config import load_config from .config import load_config
from .config import save_config from .config import save_config
from .config import verify_config_colors from .config import verify_config_colors
from .exception import UserAbort
from .prompt import yesno from .prompt import yesno
from .upgrade import is_old_version from .upgrade import is_old_version
@ -72,32 +71,16 @@ def load_or_install_jrnl(alt_config_path):
config = load_config(config_path) config = load_config(config_path)
if is_old_version(config_path): if is_old_version(config_path):
from . import upgrade from jrnl import upgrade
try: upgrade.upgrade_jrnl(config_path)
upgrade.upgrade_jrnl(config_path)
except upgrade.UpgradeValidationException:
print("Aborting upgrade.", file=sys.stderr)
print(
"Please tell us about this problem at the following URL:",
file=sys.stderr,
)
print(
"https://github.com/jrnl-org/jrnl/issues/new?title=UpgradeValidationException",
file=sys.stderr,
)
print("Exiting.", file=sys.stderr)
sys.exit(1)
upgrade_config(config, alt_config_path) upgrade_config(config, alt_config_path)
verify_config_colors(config) verify_config_colors(config)
else: else:
logging.debug("Configuration file not found, installing jrnl...") logging.debug("Configuration file not found, installing jrnl...")
try: config = install()
config = install()
except KeyboardInterrupt:
raise UserAbort("Installation aborted")
logging.debug('Using configuration "%s"', config) logging.debug('Using configuration "%s"', config)
return config return config

View file

@ -10,10 +10,13 @@ from .EncryptedJournal import EncryptedJournal
from .config import is_config_json from .config import is_config_json
from .config import load_config from .config import load_config
from .config import scope_config from .config import scope_config
from .exception import UpgradeValidationException
from .exception import UserAbort
from .prompt import yesno from .prompt import yesno
from jrnl.output import print_msg
from jrnl.output import Message
from jrnl.exception import JrnlException
from jrnl.exception import JrnlExceptionMessage
def backup(filename, binary=False): def backup(filename, binary=False):
print(f" Created a backup at {filename}.backup", file=sys.stderr) print(f" Created a backup at {filename}.backup", file=sys.stderr)
@ -27,13 +30,9 @@ def backup(filename, binary=False):
backup.write(contents) backup.write(contents)
except FileNotFoundError: except FileNotFoundError:
print(f"\nError: {filename} does not exist.") print(f"\nError: {filename} does not exist.")
try: cont = yesno(f"\nCreate {filename}?", default=False)
cont = yesno(f"\nCreate {filename}?", default=False) if not cont:
if not cont: raise JrnlException(JrnlExceptionMessage.UpgradeAborted)
raise KeyboardInterrupt
except KeyboardInterrupt:
raise UserAbort("jrnl NOT upgraded, exiting.")
def check_exists(path): def check_exists(path):
@ -121,12 +120,9 @@ older versions of jrnl anymore.
file=sys.stderr, file=sys.stderr,
) )
try: cont = yesno("\nContinue upgrading jrnl?", default=False)
cont = yesno("\nContinue upgrading jrnl?", default=False) if not cont:
if not cont: raise JrnlException(JrnlExceptionMessage.UpgradeAborted)
raise KeyboardInterrupt
except KeyboardInterrupt:
raise UserAbort("jrnl NOT upgraded, exiting.")
for journal_name, path in encrypted_journals.items(): for journal_name, path in encrypted_journals.items():
print( print(
@ -154,15 +150,13 @@ older versions of jrnl anymore.
failed_journals = [j for j in all_journals if not j.validate_parsing()] failed_journals = [j for j in all_journals if not j.validate_parsing()]
if len(failed_journals) > 0: if len(failed_journals) > 0:
print( print_msg("Aborting upgrade.", msg=Message.NORMAL)
"\nThe following journal{} failed to upgrade:\n{}".format(
"s" if len(failed_journals) > 1 else "",
"\n".join(j.name for j in failed_journals),
),
file=sys.stderr,
)
raise UpgradeValidationException raise JrnlException(
JrnlExceptionMessage.JournalFailedUpgrade,
s="s" if len(failed_journals) > 1 else "",
failed_journals="\n".join(j.name for j in failed_journals),
)
# write all journals - or - don't # write all journals - or - don't
for j in all_journals: for j in all_journals: