diff --git a/pulp_deb/app/tasks/synchronizing.py b/pulp_deb/app/tasks/synchronizing.py index aa95f29b7..3ee18c6c1 100644 --- a/pulp_deb/app/tasks/synchronizing.py +++ b/pulp_deb/app/tasks/synchronizing.py @@ -18,7 +18,7 @@ from django.conf import settings from django.db.utils import IntegrityError -from pulpcore.plugin.exceptions import DigestValidationError +from pulpcore.plugin.exceptions import DigestValidationError, PulpException from rest_framework.exceptions import ValidationError @@ -80,11 +80,32 @@ log = logging.getLogger(__name__) -class NoReleaseFile(Exception): +class DebException(PulpException): + """ + Base class for expected sync failures. + """ + + error_code = "DEB0000" + + def __init__(self, message, details=None): + try: + super().__init__() + except TypeError: + super().__init__(self.error_code) + self.message = message + self.details = details + + def __str__(self): + return f"[{self.error_code}] {self.message}" + + +class NoReleaseFile(DebException): """ Exception to signal, that no file representing a release is present. """ + error_code = "DEB0001" + def __init__(self, url, *args, **kwargs): """ Exception to signal, that no file representing a release is present. @@ -92,32 +113,32 @@ def __init__(self, url, *args, **kwargs): super().__init__( "Could not find a Release file at '{}', try checking the 'url' and " "'distributions' option on your remote".format(url), - *args, - **kwargs, ) -class NoValidSignatureForKey(Exception): +class NoValidSignatureForKey(DebException): """ Exception to signal, that verification of release file with provided GPG key fails. """ + error_code = "DEB0002" + def __init__(self, url, *args, **kwargs): """ Exception to signal, that verification of release file with provided GPG key fails. """ super().__init__( "Unable to verify any Release files from '{}' using the GPG key provided.".format(url), - *args, - **kwargs, ) -class NoPackageIndexFile(Exception): +class NoPackageIndexFile(DebException): """ Exception to signal, that no file representing a package index is present. """ + error_code = "DEB0003" + def __init__(self, relative_dir, *args, **kwargs): """ Exception to signal, that no file representing a package index is present. @@ -129,31 +150,33 @@ def __init__(self, relative_dir, *args, **kwargs): "(ignore_missing_package_indices='True') or system wide " "(FORCE_IGNORE_MISSING_PACKAGE_INDICES setting)." ) - super().__init__(_(message).format(relative_dir), *args, **kwargs) - - pass + super().__init__(_(message).format(relative_dir)) -class MissingReleaseFileField(Exception): +class MissingReleaseFileField(DebException): """ Exception signifying that the upstream release file is missing a required field. """ + error_code = "DEB0004" + def __init__(self, distribution, field, *args, **kwargs): """ The upstream release file is missing a required field. """ message = "The release file for distribution '{}' is missing the required field '{}'." - super().__init__(_(message).format(distribution, field), *args, **kwargs) + super().__init__(_(message).format(distribution, field)) -class UnknownNoSupportForArchitectureAllValue(Exception): +class UnknownNoSupportForArchitectureAllValue(DebException): """ Exception Signifying that the Release file contains the 'No-Support-for-Architecture-all' field, but with a value other than 'Packages'. We interpret this as an error since this would likely signify some unknown repo format, that pulp_deb is more likely to get wrong than right! """ + error_code = "DEB0005" + def __init__(self, release_file_path, unknown_value, *args, **kwargs): message = ( "The Release file at '{}' contains the 'No-Support-for-Architecture-all' field, with " @@ -161,9 +184,7 @@ def __init__(self, release_file_path, unknown_value, *args, **kwargs): "this field, please open an issue at https://github.com/pulp/pulp_deb/issues " "specifying the remote you are attempting to sync, so that we can improve pulp_deb!" ) - super().__init__(_(message).format(unknown_value), *args, **kwargs) - - pass + super().__init__(_(message).format(release_file_path, unknown_value)) def synchronize(remote_pk, repository_pk, mirror, optimize): diff --git a/pulp_deb/tests/functional/api/test_duplicate_packages.py b/pulp_deb/tests/functional/api/test_duplicate_packages.py index ea0fba8d6..e25aceb6b 100644 --- a/pulp_deb/tests/functional/api/test_duplicate_packages.py +++ b/pulp_deb/tests/functional/api/test_duplicate_packages.py @@ -18,6 +18,7 @@ from pulp_deb.tests.functional.utils import ( get_counts_from_content_summary, get_local_package_absolute_path, + get_task_error_message, ) DUPLICATE_PACKAGE_DIR = "data/packages/duplicates/" # below pulp_deb/tests/functional/ @@ -132,6 +133,5 @@ def test_add_duplicates_to_repo( deb_modify_repository(repository, {"add_content_units": [href1, href2]}) # Assert the error message. - assert "Cannot create repository version since there are newly added packages with" in str( - exception.value.task.error["description"] - ) + msg = get_task_error_message(exception.value.task.error) + assert "Cannot create repository version since there are newly added packages with" in msg diff --git a/pulp_deb/tests/functional/api/test_sync.py b/pulp_deb/tests/functional/api/test_sync.py index 10255a418..a58ae1d42 100644 --- a/pulp_deb/tests/functional/api/test_sync.py +++ b/pulp_deb/tests/functional/api/test_sync.py @@ -21,7 +21,7 @@ DEB_REPORT_CODE_SKIP_COMPLETE, DEB_SIGNING_KEY, ) -from pulp_deb.tests.functional.utils import get_counts_from_content_summary +from pulp_deb.tests.functional.utils import get_counts_from_content_summary, get_task_error_message @pytest.mark.parallel @@ -121,7 +121,7 @@ def test_sync_missing_package_indices( @pytest.mark.parametrize( "repo_name, remote_args, expected", [ - ("http://i-am-an-invalid-url.com/invalid/", {}, ["Cannot connect"]), + ("http://i-am-an-invalid-url.com/invalid/", {}, ["cannot connect"]), ( DEB_FIXTURE_STANDARD_REPOSITORY_NAME, {"distributions": "no_dist"}, @@ -162,8 +162,9 @@ def test_sync_invalid_cases( # Verify a PulpTaskError is raised and the error message is as expected with pytest.raises(PulpTaskError) as exc: deb_sync_repository(remote, repo) + msg = get_task_error_message(exc.value.task.error).lower() for exp in expected: - assert exp in str(exc.value.task.error["description"]) + assert exp.lower() in msg @pytest.mark.parallel diff --git a/pulp_deb/tests/functional/utils.py b/pulp_deb/tests/functional/utils.py index 40d883ef6..8e19fdbdb 100644 --- a/pulp_deb/tests/functional/utils.py +++ b/pulp_deb/tests/functional/utils.py @@ -109,3 +109,9 @@ def get_counts_from_content_summary(content_summary): for key in content: content[key] = content[key]["count"] return content + + +def get_task_error_message(error): + if not error: + return "" + return str(error.get("description") or error.get("detail") or error.get("message") or error)