Skip to content

Commit

Permalink
Improve error messages when Pulp encounters digest / size validation …
Browse files Browse the repository at this point in the history
…error

closes pulp#2244
  • Loading branch information
dralley committed Apr 18, 2022
1 parent 38fa024 commit 2b38d74
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGES/2244.misc
@@ -0,0 +1 @@
Improve error messages when Pulp encounters a checksum or size validation error.
3 changes: 3 additions & 0 deletions CHANGES/plugin_api/2244.removal
@@ -0,0 +1,3 @@
Constructor signature of `DigestValidationError` and `SizeValidationError` has changed - the
"actual" and "expected" values are now required and "url" which was previously a positional
argument is now a keyword argument.
14 changes: 8 additions & 6 deletions pulpcore/app/models/content.py
Expand Up @@ -330,7 +330,7 @@ def init_and_validate(file, expected_digests=None, expected_size=None):

if expected_size:
if size != expected_size:
raise SizeValidationError()
raise SizeValidationError(size, expected_size)

if expected_digests:
for algorithm, expected_digest in expected_digests.items():
Expand All @@ -340,8 +340,9 @@ def init_and_validate(file, expected_digests=None, expected_size=None):
algorithm
)
)
if expected_digest != hashers[algorithm].hexdigest():
raise DigestValidationError()
actual_digest = hashers[algorithm].hexdigest()
if expected_digest != actual_digest:
raise DigestValidationError(actual_digest, expected_digest)

attributes = {"size": size, "file": file}
for algorithm in Artifact.DIGEST_FIELDS:
Expand Down Expand Up @@ -443,7 +444,7 @@ def init_and_validate(file, expected_digests=None, expected_size=None):

if expected_size:
if size != expected_size:
raise SizeValidationError()
raise SizeValidationError(size, expected_size)

if expected_digests:
for algorithm, expected_digest in expected_digests.items():
Expand All @@ -453,8 +454,9 @@ def init_and_validate(file, expected_digests=None, expected_size=None):
algorithm
)
)
if expected_digest != hashers[algorithm].hexdigest():
raise DigestValidationError()
actual_digest = hashers[algorithm].hexdigest()
if expected_digest != actual_digest:
raise DigestValidationError(actual_digest, expected_digest)

return PulpTemporaryFile(file=file)

Expand Down
11 changes: 7 additions & 4 deletions pulpcore/download/base.py
Expand Up @@ -219,8 +219,9 @@ def validate_digests(self):
"""
if self.expected_digests:
for algorithm, expected_digest in self.expected_digests.items():
if expected_digest != self._digests[algorithm].hexdigest():
raise DigestValidationError(self.url)
actual_digest = self._digests[algorithm].hexdigest()
if actual_digest != expected_digest:
raise DigestValidationError(actual_digest, expected_digest, url=self.url)

def validate_size(self):
"""
Expand All @@ -232,8 +233,10 @@ def validate_size(self):
:meth:`~pulpcore.plugin.download.BaseDownloader.handle_data`.
"""
if self.expected_size:
if self._size != self.expected_size:
raise SizeValidationError(self.url)
actual_size = self._size
expected_size = self.expected_size
if actual_size != expected_size:
raise SizeValidationError(actual_size, expected_size, url=self.url)

async def run(self, extra_data=None):
"""
Expand Down
42 changes: 29 additions & 13 deletions pulpcore/exceptions/validation.py
Expand Up @@ -16,33 +16,49 @@ class DigestValidationError(ValidationError):
Raised when a file fails to validate a digest checksum.
"""

def __init__(self, *args):
def __init__(self, actual, expected, *args, url=None, **kwargs):
super().__init__("PLP0003")
if args:
self.url = args[0]
self.url = url
self.actual = actual
self.expected = expected

def __str__(self):
if hasattr(self, "url"):
return _("A file located at the url {} failed validation due to checksum.").format(
self.url
if self.url:
msg = _(
"A file located at the url {url} failed validation due to checksum. "
"Expected '{expected}', Actual '{actual}'"
)
return _("A file failed validation due to checksum.")
return msg.format(url=self.url, expected=self.expected, actual=self.actual)
else:
msg = _(
"A file failed validation due to checksum. Expected '{expected}', Actual '{actual}'"
)
return msg.format(expected=self.expected, actual=self.actual)


class SizeValidationError(ValidationError):
"""
Raised when a file fails to validate a size checksum.
"""

def __init__(self, *args):
def __init__(self, actual, expected, *args, url=None, **kwargs):
super().__init__("PLP0004")
if args:
self.url = args[0]
self.url = url
self.actual = actual
self.expected = expected

def __str__(self):
if hasattr(self, "url"):
return _("A file located at the url {} failed validation due to size.").format(self.url)
return _("A file failed validation due to size.")
if self.url:
msg = _(
"A file located at the url {url} failed validation due to size. "
"Expected '{expected}', Actual '{actual}'"
)
return msg.format(url=self.url, expected=self.expected, actual=self.actual)
else:
msg = _(
"A file failed validation due to size. Expected '{expected}', Actual '{actual}'"
)
return msg.format(expected=self.expected, actual=self.actual)


class MissingDigestValidationError(Exception):
Expand Down

0 comments on commit 2b38d74

Please sign in to comment.