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.
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user