diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ psutil requests iso8601 +goto-statement pkginfo python-debian python-dateutil diff --git a/swh/loader/package/loader.py b/swh/loader/package/loader.py --- a/swh/loader/package/loader.py +++ b/swh/loader/package/loader.py @@ -28,6 +28,7 @@ ) import attr +from goto import with_goto import sentry_sdk from swh.core.tarball import uncompress @@ -464,6 +465,7 @@ return result + @with_goto def load(self) -> Dict: """Load for a specific origin the associated contents. @@ -538,14 +540,8 @@ logger.debug("known artifacts: %s", known_artifacts) except Exception as e: logger.exception("Failed to get previous state for %s", self.url) - sentry_sdk.capture_exception(e) - return self.finalize_visit( - snapshot=snapshot, - visit=visit, - failed_branches=failed_branches, - status_visit="failed", - status_load="failed", - ) + status_visit = "failed" + goto .fail load_exceptions: List[Exception] = [] @@ -561,13 +557,8 @@ status_load="failed", ) except Exception: - return self.finalize_visit( - snapshot=snapshot, - visit=visit, - failed_branches=failed_branches, - status_visit="failed", - status_load="failed", - ) + status_visit="failed", + goto .fail # Get the metadata of each version's package packages_info: List[Tuple[str, str, TPackageInfo]] = [ @@ -640,13 +631,8 @@ if not tmp_revisions: # We could not load any revisions; fail completely - return self.finalize_visit( - snapshot=snapshot, - visit=visit, - failed_branches=failed_branches, - status_visit="failed", - status_load="failed", - ) + status_visit="failed", + goto .fail try: # Retrieve the default release version (the "latest" one) @@ -662,9 +648,8 @@ self.storage.flush() except Exception as e: logger.exception("Failed to build snapshot for origin %s", self.url) - sentry_sdk.capture_exception(e) status_visit = "failed" - status_load = "failed" + goto .fail if snapshot: try: @@ -674,9 +659,8 @@ logger.exception( "Failed to load extrinsic snapshot metadata for %s", self.url ) - sentry_sdk.capture_exception(e) status_visit = "partial" - status_load = "failed" + goto .fail try: metadata_objects = self.build_extrinsic_origin_metadata() @@ -685,9 +669,9 @@ logger.exception( "Failed to load extrinsic origin metadata for %s", self.url ) - sentry_sdk.capture_exception(e) status_visit = "partial" status_load = "failed" + goto .fail return self.finalize_visit( snapshot=snapshot, @@ -697,6 +681,16 @@ status_load=status_load, ) + label .fail + sentry_sdk.capture_exception(e) + return self.finalize_visit( + snapshot=snapshot, + visit=visit, + failed_branches=failed_branches, + status_visit=status_visit, + status_load="failed", + ) + def _load_directory( self, dl_artifacts: List[Tuple[str, Mapping[str, Any]]], tmpdir: str ) -> Tuple[str, from_disk.Directory]: