Skip to content

Commit 4394594

Browse files
committed
Add more Pulp Exceptions.
Assisted-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent e1e023c commit 4394594

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add more Pulp Exceptions.

pulp_python/app/exceptions.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from gettext import gettext as _
2+
3+
from pulpcore.plugin.exceptions import PulpException
4+
5+
6+
class ProvenanceVerificationError(PulpException):
7+
"""
8+
Raised when provenance verification fails.
9+
"""
10+
11+
error_code = "PLPY0001"
12+
13+
def __init__(self, message):
14+
"""
15+
:param message: Description of the provenance verification error
16+
:type message: str
17+
"""
18+
self.message = message
19+
20+
def __str__(self):
21+
return f"[{self.error_code}] " + _("Provenance verification failed: {message}").format(
22+
message=self.message
23+
)
24+
25+
26+
class AttestationVerificationError(PulpException):
27+
"""
28+
Raised when attestation verification fails.
29+
"""
30+
31+
error_code = "PLPY0002"
32+
33+
def __init__(self, message):
34+
"""
35+
:param message: Description of the attestation verification error
36+
:type message: str
37+
"""
38+
self.message = message
39+
40+
def __str__(self):
41+
return f"[{self.error_code}] " + _("Attestation verification failed: {message}").format(
42+
message=self.message
43+
)

pulp_python/app/serializers.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
from packaging.requirements import Requirement
99
from rest_framework import serializers
1010
from pypi_attestations import AttestationError
11-
from pydantic import TypeAdapter, ValidationError
11+
from pydantic import TypeAdapter, ValidationError as PydanticValidationError
1212
from urllib.parse import urljoin
1313

14+
from pulpcore.plugin.exceptions import DigestValidationError, ValidationError
1415
from pulpcore.plugin import models as core_models
1516
from pulpcore.plugin import serializers as core_serializers
1617
from pulpcore.plugin.util import get_domain, get_prn, get_current_authenticated_user
1718

1819
from pulp_python.app import models as python_models
20+
from pulp_python.app.exceptions import AttestationVerificationError, ProvenanceVerificationError
1921
from pulp_python.app.provenance import (
2022
Attestation,
2123
Provenance,
@@ -374,7 +376,7 @@ def validate_attestations(self, value):
374376
attestations = TypeAdapter(list[Attestation]).validate_json(value)
375377
else:
376378
attestations = TypeAdapter(list[Attestation]).validate_python(value)
377-
except ValidationError as e:
379+
except PydanticValidationError as e:
378380
raise serializers.ValidationError(_("Invalid attestations: {}".format(e)))
379381
return attestations
380382

@@ -387,9 +389,7 @@ def handle_attestations(self, filename, sha256, attestations, offline=True):
387389
try:
388390
verify_provenance(filename, sha256, provenance, offline=offline)
389391
except AttestationError as e:
390-
raise serializers.ValidationError(
391-
{"attestations": _("Attestations failed verification: {}".format(e))}
392-
)
392+
raise AttestationVerificationError(str(e))
393393
return provenance.model_dump(mode="json")
394394

395395
def deferred_validate(self, data):
@@ -408,26 +408,23 @@ def deferred_validate(self, data):
408408
try:
409409
filename = data["relative_path"]
410410
except KeyError:
411-
raise serializers.ValidationError(detail={"relative_path": _("This field is required")})
411+
raise ValidationError(_("This field is required: relative_path"))
412412

413413
artifact = data["artifact"]
414414
try:
415415
_data = artifact_to_python_content_data(filename, artifact, domain=get_domain())
416416
except ValueError:
417-
raise serializers.ValidationError(
417+
raise ValidationError(
418418
_(
419419
"Extension on {} is not a valid python extension "
420420
"(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)"
421421
).format(filename)
422422
)
423423

424424
if data.get("sha256") and data["sha256"] != artifact.sha256:
425-
raise serializers.ValidationError(
426-
detail={
427-
"sha256": _(
428-
"The uploaded artifact's sha256 checksum does not match the one provided"
429-
)
430-
}
425+
raise DigestValidationError(
426+
actual=artifact.sha256,
427+
expected=data["sha256"],
431428
)
432429

433430
data.update(_data)
@@ -641,15 +638,13 @@ def deferred_validate(self, data):
641638
try:
642639
provenance = Provenance.model_validate_json(data["file"].read())
643640
data["provenance"] = provenance.model_dump(mode="json")
644-
except ValidationError as e:
645-
raise serializers.ValidationError(
646-
_("The uploaded provenance is not valid: {}".format(e))
647-
)
641+
except PydanticValidationError as e:
642+
raise ValidationError(_("The uploaded provenance is not valid: {}".format(e)))
648643
if data.pop("verify"):
649644
try:
650645
verify_provenance(data["package"].filename, data["package"].sha256, provenance)
651646
except AttestationError as e:
652-
raise serializers.ValidationError(_("Provenance verification failed: {}".format(e)))
647+
raise ProvenanceVerificationError(str(e))
653648
return data
654649

655650
def retrieve(self, validated_data):

pulp_python/tests/functional/api/test_attestations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_verify_provenance(python_bindings, twine_package, python_content_factor
6969
with pytest.raises(PulpTaskError) as e:
7070
monitor_task(provenance.task)
7171
assert e.value.task.state == "failed"
72-
assert "twine-6.2.0-py3-none-any.whl != twine-6.2.0.tar.gz" in e.value.task.error["description"]
72+
assert "[PLPY0002]" in e.value.task.error["description"]
7373

7474
# Test creating a provenance without verifying
7575
provenance = python_bindings.ContentProvenanceApi.create(
@@ -239,4 +239,4 @@ def test_bad_attestation_upload(python_bindings, twine_package, monitor_task):
239239
with pytest.raises(PulpTaskError) as e:
240240
monitor_task(task)
241241
assert e.value.task.state == "failed"
242-
assert "Attestations failed verification" in e.value.task.error["description"]
242+
assert "[PLPY0002]" in e.value.task.error["description"]

0 commit comments

Comments
 (0)