From adfded92d0c80da761839f57f822a2361ad5eee2 Mon Sep 17 00:00:00 2001 From: furtest Date: Fri, 30 Jan 2026 17:40:41 +0100 Subject: [PATCH] synchroniser : raise error instead of returning a value Raise a FatalSyncError when the synchronisation fails instead of returning the unison return code. --- src/unisync/synchroniser.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/unisync/synchroniser.py b/src/unisync/synchroniser.py index 42e79c2..d5b689a 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, UnknownSSHError +from unisync.errors import RemoteMountedError, InvalidMountError, UnknownSSHError, FatalSyncError from unisync.config import BackupConfig logger = logging.getLogger(__name__) @@ -105,9 +105,6 @@ class Synchroniser: connection_timeout: Time given to the user to authenticate to the remote server. On slow connections one might want to increase this. - Returns: - An error code (0 success, 1 TimeoutExpired, 2 KeyboardInterrupt). - TODO change that to raising the exception. Raises: subprocess.TimeoutExpired: @@ -156,18 +153,18 @@ class Synchroniser: close = subprocess.Popen(command) return close.wait() - def sync_files(self, paths:list, force:bool=False) -> int: + def sync_files(self, paths:list, force:bool=False) -> None: """Synchronises the files. Args: paths: List of paths to synchronise. force: Force the changes from remote to local. - Returns: - The return code of sync. + Raises: + FatalSyncError: A fatal error occured during the synchronisation. """ - return self.sync( + self.sync( f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/.data", self.local, paths=paths, @@ -175,16 +172,16 @@ class Synchroniser: other=self.files_extra ) - def sync_links(self, ignore:list) -> int: + def sync_links(self, ignore:list) -> None: """Synchronises the links, they must exist already. Args: ignore: List of paths to ignore. - Returns: - The return code of sync. + Raises: + FatalSyncError: A fatal error occured during the synchronisation. """ - return self.sync( + self.sync( f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/links", self.local, ignore=ignore, @@ -194,7 +191,7 @@ class Synchroniser: def sync(self, remote_root:str, local_root:str, paths:list=[], ignore:list=[], force:bool=False, other:list=[] - ) -> int: + ) -> None: """Performs the synchronisation by calling unison. Args: @@ -213,8 +210,11 @@ class Synchroniser: They will be added to the command as is no - in front. For exemple backups are implemented using this argument. - Returns: - the unison return code see section 6.11 of the documentation + Raises: + FatalSyncError: + If unison returns 3 it means either a fatal error occured or the synchronisation + was interrupted. + If this happens propagate the error to unisync. """ command = [ "/usr/bin/unison", "-root", remote_root, "-root", local_root ] for arg in self.args_bool: @@ -247,7 +247,8 @@ class Synchroniser: proc = subprocess.Popen(command) ret_code = proc.wait() - return ret_code + if ret_code == 3: + raise FatalSyncError("Synchronisation could not be completed") def update_links(self, background:bool=True): """Updates the links on the remote.