99from drf_spectacular .utils import extend_schema_serializer
1010from packaging .requirements import Requirement
1111from packaging .version import InvalidVersion , Version
12- from pydantic import TypeAdapter , ValidationError
12+ from pydantic import TypeAdapter , ValidationError as PydanticValidationError
1313from pypi_attestations import AttestationError
1414from rest_framework import serializers
1515
16+ from pulpcore .plugin .exceptions import DigestValidationError
1617from pulpcore .plugin import models as core_models
1718from pulpcore .plugin import serializers as core_serializers
1819from pulpcore .plugin .util import get_current_authenticated_user , get_domain , get_prn , reverse
1920
2021from pulp_python .app import models as python_models
22+ from pulp_python .app .exceptions import (
23+ AttestationVerificationError ,
24+ InvalidProvenanceError ,
25+ InvalidPythonExtensionError ,
26+ MissingRelativePathError ,
27+ ProvenanceVerificationError ,
28+ )
2129from pulp_python .app .provenance import (
2230 AnyPublisher ,
2331 Attestation ,
@@ -387,8 +395,8 @@ def validate_attestations(self, value):
387395 attestations = TypeAdapter (list [Attestation ]).validate_json (value )
388396 else :
389397 attestations = TypeAdapter (list [Attestation ]).validate_python (value )
390- except ValidationError as e :
391- raise serializers .ValidationError (_ ("Invalid attestations: {}" ) .format (e ))
398+ except PydanticValidationError as e :
399+ raise serializers .ValidationError (_ ("Invalid attestations: {}" .format (e ) ))
392400 return attestations
393401
394402 def handle_attestations (self , filename , sha256 , attestations , offline = True ):
@@ -400,9 +408,7 @@ def handle_attestations(self, filename, sha256, attestations, offline=True):
400408 try :
401409 verify_provenance (filename , sha256 , provenance , offline = offline )
402410 except AttestationError as e :
403- raise serializers .ValidationError (
404- {"attestations" : _ ("Attestations failed verification: {}" ).format (e )}
405- )
411+ raise AttestationVerificationError (str (e ))
406412 return provenance .model_dump (mode = "json" )
407413
408414 def deferred_validate (self , data ):
@@ -421,26 +427,18 @@ def deferred_validate(self, data):
421427 try :
422428 filename = data ["relative_path" ]
423429 except KeyError :
424- raise serializers . ValidationError ( detail = { "relative_path" : _ ( "This field is required" )} )
430+ raise MissingRelativePathError ( )
425431
426432 artifact = data ["artifact" ]
427433 try :
428434 _data = artifact_to_python_content_data (filename , artifact , domain = get_domain ())
429435 except ValueError :
430- raise serializers .ValidationError (
431- _ (
432- "Extension on {} is not a valid python extension "
433- "(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)"
434- ).format (filename )
435- )
436+ raise InvalidPythonExtensionError (filename )
436437
437438 if data .get ("sha256" ) and data ["sha256" ] != artifact .sha256 :
438- raise serializers .ValidationError (
439- detail = {
440- "sha256" : _ (
441- "The uploaded artifact's sha256 checksum does not match the one provided"
442- )
443- }
439+ raise DigestValidationError (
440+ actual = artifact .sha256 ,
441+ expected = data ["sha256" ],
444442 )
445443
446444 data .update (_data )
@@ -654,15 +652,13 @@ def deferred_validate(self, data):
654652 try :
655653 provenance = Provenance .model_validate_json (data ["file" ].read ())
656654 data ["provenance" ] = provenance .model_dump (mode = "json" )
657- except ValidationError as e :
658- raise serializers .ValidationError (
659- _ ("The uploaded provenance is not valid: {}" ).format (e )
660- )
655+ except PydanticValidationError as e :
656+ raise InvalidProvenanceError (str (e ))
661657 if data .pop ("verify" ):
662658 try :
663659 verify_provenance (data ["package" ].filename , data ["package" ].sha256 , provenance )
664660 except AttestationError as e :
665- raise serializers . ValidationError ( _ ( "Provenance verification failed: {}" ). format (e ))
661+ raise ProvenanceVerificationError ( str (e ))
666662 return data
667663
668664 def retrieve (self , validated_data ):
0 commit comments