From aa9343a75c3e107194c9e5415da6ab8929e7efc6 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 19 Feb 2022 18:07:13 -0800 Subject: [PATCH] Move error for too many wrong passwords into new handling --- jrnl/EncryptedJournal.py | 27 +++++++++++++-------------- jrnl/exception.py | 4 ++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/jrnl/EncryptedJournal.py b/jrnl/EncryptedJournal.py index 7354e7a2..4440783e 100644 --- a/jrnl/EncryptedJournal.py +++ b/jrnl/EncryptedJournal.py @@ -21,6 +21,9 @@ from .Journal import Journal from .Journal import LegacyJournal from .prompt import create_password +from jrnl.exception import JrnlException +from jrnl.exception import JrnlExceptionMessage + def make_key(password): password = password.encode("utf-8") @@ -53,11 +56,11 @@ def decrypt_content( password = getpass.getpass() result = decrypt_func(password) attempt += 1 - if result is not None: - return result - else: - print("Extremely wrong password.", file=sys.stderr) - sys.exit(1) + + if result is None: + raise JrnlException(JrnlExceptionMessage.PasswordMaxTriesExceeded) + + return result class EncryptedJournal(Journal): @@ -121,15 +124,11 @@ class EncryptedJournal(Journal): @classmethod def from_journal(cls, other: Journal): new_journal = super().from_journal(other) - try: - new_journal.password = ( - other.password - if hasattr(other, "password") - else create_password(other.name) - ) - except KeyboardInterrupt: - print("[Interrupted while creating new journal]", file=sys.stderr) - sys.exit(1) + new_journal.password = ( + other.password + if hasattr(other, "password") + else create_password(other.name) + ) return new_journal diff --git a/jrnl/exception.py b/jrnl/exception.py index d8949fa4..92a9e06a 100644 --- a/jrnl/exception.py +++ b/jrnl/exception.py @@ -76,6 +76,10 @@ class JrnlExceptionMessage(Enum): {config_file} """ + PasswordMaxTriesExceeded = """ + Too many attempts with wrong password. + """ + SomeTest = """ Some error or something