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
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,6 +45,18 @@ 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

View file

@ -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_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")
logging.debug('Using configuration "%s"', config)
return config

View file

@ -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.")
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.")
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: