From 16316ea0eb0a5e2c0dfaa83d4a25f6107e40bd15 Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Tue, 17 Jun 2025 19:27:27 -0700 Subject: [PATCH] sync: Share final error handling logic between sync modes Bug: 421935613 Change-Id: I4604a83943dbbd71d979158d7a1c4b8c243347d2 --- subcmds/sync.py | 61 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/subcmds/sync.py b/subcmds/sync.py index dd0cfc51b..74d3f170e 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py @@ -2129,29 +2129,48 @@ later is required to fix a server side protocol bug. # If we saw an error, exit with code 1 so that other scripts can check. if err_event.is_set(): + self._ReportSyncErrors( + errors, + err_network=err_network_sync, + err_checkout=err_checkout, + err_update_projects=err_update_projects, + err_update_linkfiles=err_update_linkfiles, + failing_checkout_projects=err_results, + ) - def print_and_log(err_msg): - self.git_event_log.ErrorEvent(err_msg) - logger.error("%s", err_msg) + def _ReportSyncErrors( + self, + errors: List[Exception], + err_network: bool, + err_checkout: bool, + err_update_projects: bool, + err_update_linkfiles: bool, + failing_checkout_projects: List[str], + ): + """Common error reporting for sync failures.""" + def print_and_log(err_msg): + self.git_event_log.ErrorEvent(err_msg) + logger.error("%s", err_msg) - print_and_log("error: Unable to fully sync the tree") - if err_network_sync: - print_and_log("error: Downloading network changes failed.") - if err_update_projects: - print_and_log("error: Updating local project lists failed.") - if err_update_linkfiles: - print_and_log("error: Updating copyfiles or linkfiles failed.") - if err_checkout: - print_and_log("error: Checking out local projects failed.") - if err_results: - # Don't log repositories, as it may contain sensitive info. - logger.error("Failing repos:\n%s", "\n".join(err_results)) - # Not useful to log. - logger.error( - 'Try re-running with "-j1 --fail-fast" to exit at the first ' - "error." - ) - raise SyncError(aggregate_errors=errors) + print_and_log("error: Unable to fully sync the tree") + if err_network: + print_and_log("error: Downloading network changes failed.") + if err_update_projects: + print_and_log("error: Updating local project lists failed.") + if err_update_linkfiles: + print_and_log("error: Updating copyfiles or linkfiles failed.") + if err_checkout: + print_and_log("error: Checking out local projects failed.") + if failing_checkout_projects: + # Don't log repositories, as it may contain sensitive info. + logger.error( + "Failing repos:\n%s", "\n".join(failing_checkout_projects) + ) + # Not useful to log. + logger.error( + 'Try re-running with "-j1 --fail-fast" to exit at the first error.' + ) + raise SyncError(aggregate_errors=errors) @classmethod def _SyncOneProject(cls, opt, project_index, project) -> _SyncResult: