From a922eaa5429abff4bdb3e2f1aaf1491946769f01 Mon Sep 17 00:00:00 2001 From: furtest Date: Tue, 20 Jan 2026 22:23:09 +0100 Subject: [PATCH] synchroniser : use exception instead of return codes In create_ssh_master_connection return codes where used instead of proper error handling with exception, replace these codes with the raising of an appropriate exception. --- src/unisync/synchroniser.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/unisync/synchroniser.py b/src/unisync/synchroniser.py index 7d0e912..42e79c2 100644 --- a/src/unisync/synchroniser.py +++ b/src/unisync/synchroniser.py @@ -16,7 +16,7 @@ import logging from pathlib import Path from typing import cast -from unisync.errors import RemoteMountedError, InvalidMountError +from unisync.errors import RemoteMountedError, InvalidMountError, UnknownSSHError from unisync.config import BackupConfig logger = logging.getLogger(__name__) @@ -92,7 +92,7 @@ class Synchroniser: f"Name {backup.backupprefix[:-1]}" ]) - def create_ssh_master_connection(self, control_path:str="~/.ssh/control_%C", connection_timeout:int=60) -> int: + def create_ssh_master_connection(self, control_path:str="~/.ssh/control_%C", connection_timeout:int=60) -> None: """Creates an ssh master connection. It is used so the user only has to authenticate once to the remote server. @@ -108,6 +108,14 @@ class Synchroniser: Returns: An error code (0 success, 1 TimeoutExpired, 2 KeyboardInterrupt). TODO change that to raising the exception. + + Raises: + subprocess.TimeoutExpired: + The user didn't finish loging in in time. + KeyboardInterrupt: + The user interrupted the process. + UnknownSSHError: + An error occured during the connection. """ self.control_path = os.path.expanduser(control_path) command = [ @@ -121,16 +129,15 @@ class Synchroniser: master_ssh = subprocess.Popen(command) try: ret_code = master_ssh.wait(timeout=connection_timeout) - except subprocess.TimeoutExpired: + except subprocess.TimeoutExpired as e: print("Time to login expired", file=sys.stderr) - return 1 - except KeyboardInterrupt: - return 2 + raise e + except KeyboardInterrupt as e: + raise e if ret_code != 0: print("Login to remote failed", file=sys.stderr) - return ret_code - return 0 + raise UnknownSSHError def close_ssh_master_connection(self) -> int: