From 44dffc272a68dd98ac5c7b4829d564d18359dbc8 Mon Sep 17 00:00:00 2001 From: anshkarwasra Date: Tue, 17 Dec 2024 10:26:52 +0530 Subject: [PATCH] error handeling --- main.py | 54 ++++++++++++++++++++++++++++++------------------------ manager.py | 2 ++ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 57ab52b..4455ab7 100644 --- a/main.py +++ b/main.py @@ -23,30 +23,36 @@ def main(): done = False while not done: choice = input("Enter choice: ").strip().lower() - if choice == '1': - path = input("Enter key file path: ").strip() - pm.create_key(path) - elif choice == '2': - path = input("Enter key file path: ").strip() - pm.load_key(path) - elif choice == '3': - path = input("Enter password file path: ").strip() - pm.create_password_file(path, password) - elif choice == '4': - path = input("Enter password file path: ").strip() - pm.load_password_file(path) - elif choice == '5': - site = input("Enter site: ").strip() - password = input("Enter password: ").strip() - pm.add_password(site, password) - elif choice == '6': - site = input("Enter site: ").strip() - print(f"Password for {site}: {pm.get_password(site)}") - elif choice == 'q': - done = True - print("Goodbye!") - else: - print("Invalid choice. Please try again.") + try: + if choice == '1': + path = input("Enter key file path: ").strip() + pm.create_key(path) + elif choice == '2': + path = input("Enter key file path: ").strip() + pm.load_key(path) + elif choice == '3': + path = input("Enter password file path: ").strip() + pm.create_password_file(path, password) + elif choice == '4': + path = input("Enter password file path: ").strip() + pm.load_password_file(path) + elif choice == '5': + site = input("Enter site: ").strip() + password = input("Enter password: ").strip() + pm.add_password(site, password) + elif choice == '6': + site = input("Enter site: ").strip() + print(f"Password for {site}: {pm.get_password(site)}") + elif choice == 'q': + done = True + print("Goodbye!") + else: + print("Invalid choice. Please try again.") + except Exception as e: + if str(e) == "KeyNotLoadedError": + print("Key not loaded. Please load a key first.") + else: + print(f"Something went wrong: {e} Please report this to the developer.") if __name__ == '__main__': diff --git a/manager.py b/manager.py index 212f675..5768877 100644 --- a/manager.py +++ b/manager.py @@ -33,6 +33,8 @@ def load_password_file(self, path): def add_password(self, site, password): self.password_dict[site] = password if self.password_file is not None: + if self.key is None: + raise Exception("KeyNotLoadedError") with open(self.password_file, 'a+') as f: encrypted = Fernet(self.key).encrypt(password.encode()).decode() f.write(f"{site}:{encrypted}\n")