From b907064639f382ad11c4771210d7f33a2a03378c Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 19 Feb 2022 16:22:22 -0800 Subject: [PATCH] move some exceptions and errors to the new exception handling --- jrnl/exception.py | 20 +++++++++++++------- jrnl/install.py | 23 +++-------------------- jrnl/upgrade.py | 40 +++++++++++++++++----------------------- 3 files changed, 33 insertions(+), 50 deletions(-) diff --git a/jrnl/exception.py b/jrnl/exception.py index e9198dbd..46a17e22 100644 --- a/jrnl/exception.py +++ b/jrnl/exception.py @@ -9,12 +9,6 @@ class UserAbort(Exception): pass -class UpgradeValidationException(Exception): - """Raised when the contents of an upgraded journal do not match the old journal""" - - pass - - class JrnlExceptionMessage(Enum): ConfigDirectoryIsFile = """ The path to your jrnl configuration directory is a file, not a directory: @@ -51,12 +45,24 @@ class JrnlExceptionMessage(Enum): 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 = """ Some error or something 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 - """ + """ class JrnlException(Exception): diff --git a/jrnl/install.py b/jrnl/install.py index b2b583cf..d0115fbf 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -14,7 +14,6 @@ from .config import get_default_journal_path from .config import load_config from .config import save_config from .config import verify_config_colors -from .exception import UserAbort from .prompt import yesno from .upgrade import is_old_version @@ -72,32 +71,16 @@ def load_or_install_jrnl(alt_config_path): config = load_config(config_path) if is_old_version(config_path): - from . import upgrade + from jrnl import upgrade - try: - 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.upgrade_jrnl(config_path) upgrade_config(config, alt_config_path) verify_config_colors(config) else: logging.debug("Configuration file not found, installing jrnl...") - try: - config = install() - except KeyboardInterrupt: - raise UserAbort("Installation aborted") + config = install() logging.debug('Using configuration "%s"', config) return config diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py index 158f8de3..a97b361e 100644 --- a/jrnl/upgrade.py +++ b/jrnl/upgrade.py @@ -10,10 +10,13 @@ from .EncryptedJournal import EncryptedJournal from .config import is_config_json from .config import load_config from .config import scope_config -from .exception import UpgradeValidationException -from .exception import UserAbort 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): print(f" Created a backup at {filename}.backup", file=sys.stderr) @@ -27,13 +30,9 @@ def backup(filename, binary=False): backup.write(contents) except FileNotFoundError: print(f"\nError: {filename} does not exist.") - try: - cont = yesno(f"\nCreate {filename}?", default=False) - if not cont: - raise KeyboardInterrupt - - except KeyboardInterrupt: - raise UserAbort("jrnl NOT upgraded, exiting.") + cont = yesno(f"\nCreate {filename}?", default=False) + if not cont: + raise JrnlException(JrnlExceptionMessage.UpgradeAborted) def check_exists(path): @@ -121,12 +120,9 @@ older versions of jrnl anymore. file=sys.stderr, ) - try: - cont = yesno("\nContinue upgrading jrnl?", default=False) - if not cont: - raise KeyboardInterrupt - except KeyboardInterrupt: - raise UserAbort("jrnl NOT upgraded, exiting.") + cont = yesno("\nContinue upgrading jrnl?", default=False) + if not cont: + raise JrnlException(JrnlExceptionMessage.UpgradeAborted) for journal_name, path in encrypted_journals.items(): print( @@ -154,15 +150,13 @@ older versions of jrnl anymore. failed_journals = [j for j in all_journals if not j.validate_parsing()] if len(failed_journals) > 0: - print( - "\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, - ) + print_msg("Aborting upgrade.", msg=Message.NORMAL) - 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 for j in all_journals: