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 pathlib import Path
|
||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
from unisync.errors import RemoteMountedError, InvalidMountError
|
from unisync.errors import RemoteMountedError, InvalidMountError, UnknownSSHError
|
||||||
from unisync.config import BackupConfig
|
from unisync.config import BackupConfig
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -92,7 +92,7 @@ class Synchroniser:
|
|||||||
f"Name {backup.backupprefix[:-1]}"
|
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.
|
"""Creates an ssh master connection.
|
||||||
|
|
||||||
It is used so the user only has to authenticate once to the remote server.
|
It is used so the user only has to authenticate once to the remote server.
|
||||||
@@ -108,6 +108,14 @@ class Synchroniser:
|
|||||||
Returns:
|
Returns:
|
||||||
An error code (0 success, 1 TimeoutExpired, 2 KeyboardInterrupt).
|
An error code (0 success, 1 TimeoutExpired, 2 KeyboardInterrupt).
|
||||||
TODO change that to raising the exception.
|
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)
|
self.control_path = os.path.expanduser(control_path)
|
||||||
command = [
|
command = [
|
||||||
@@ -121,16 +129,15 @@ class Synchroniser:
|
|||||||
master_ssh = subprocess.Popen(command)
|
master_ssh = subprocess.Popen(command)
|
||||||
try:
|
try:
|
||||||
ret_code = master_ssh.wait(timeout=connection_timeout)
|
ret_code = master_ssh.wait(timeout=connection_timeout)
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired as e:
|
||||||
print("Time to login expired", file=sys.stderr)
|
print("Time to login expired", file=sys.stderr)
|
||||||
return 1
|
raise e
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt as e:
|
||||||
return 2
|
raise e
|
||||||
|
|
||||||
if ret_code != 0:
|
if ret_code != 0:
|
||||||
print("Login to remote failed", file=sys.stderr)
|
print("Login to remote failed", file=sys.stderr)
|
||||||
return ret_code
|
raise UnknownSSHError
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
def close_ssh_master_connection(self) -> int:
|
def close_ssh_master_connection(self) -> int:
|
||||||
|
|||||||
Reference in New Issue
Block a user