@@ -774,26 +774,17 @@ def hf_raise_for_status(response: httpx.Response, endpoint_name: str | None = No
774774
775775 if error_code == "RevisionNotFound" :
776776 message = f"{ response .status_code } Client Error." + "\n \n " + f"Revision Not Found for url: { response .url } ."
777- revision_err = _format (RevisionNotFoundError , message , response )
778- revision_err .repo_type = repo_type
779- revision_err .repo_id = repo_id
780- raise revision_err from e
777+ raise _format (RevisionNotFoundError , message , response , repo_type = repo_type , repo_id = repo_id ) from e
781778
782779 elif error_code == "EntryNotFound" :
783780 message = f"{ response .status_code } Client Error." + "\n \n " + f"Entry Not Found for url: { response .url } ."
784- entry_err = _format (RemoteEntryNotFoundError , message , response )
785- entry_err .repo_type = repo_type
786- entry_err .repo_id = repo_id
787- raise entry_err from e
781+ raise _format (RemoteEntryNotFoundError , message , response , repo_type = repo_type , repo_id = repo_id ) from e
788782
789783 elif error_code == "GatedRepo" :
790784 message = (
791785 f"{ response .status_code } Client Error." + "\n \n " + f"Cannot access gated repo for url { response .url } ."
792786 )
793- gated_err = _format (GatedRepoError , message , response )
794- gated_err .repo_type = repo_type
795- gated_err .repo_id = repo_id
796- raise gated_err from e
787+ raise _format (GatedRepoError , message , response , repo_type = repo_type , repo_id = repo_id ) from e
797788
798789 elif error_message == "Access to this resource is disabled." :
799790 message = (
@@ -817,9 +808,9 @@ def hf_raise_for_status(response: httpx.Response, endpoint_name: str | None = No
817808 + "\n Please make sure you specified the correct bucket id (namespace/name)."
818809 + "\n If the bucket is private, make sure you are authenticated and your token has the required permissions."
819810 )
820- bucket_err = _format (BucketNotFoundError , message , response )
821- bucket_err . bucket_id = _parse_bucket_id_from_url (request_url )
822- raise bucket_err from e
811+ raise _format (
812+ BucketNotFoundError , message , response , bucket_id = _parse_bucket_id_from_url (request_url )
813+ ) from e
823814
824815 elif error_code == "RepoNotFound" or (
825816 response .status_code == 401
@@ -841,10 +832,7 @@ def hf_raise_for_status(response: httpx.Response, endpoint_name: str | None = No
841832 " make sure you are authenticated and your token has the required permissions."
842833 + "\n For more details, see https://huggingface.co/docs/huggingface_hub/authentication"
843834 )
844- repo_err = _format (RepositoryNotFoundError , message , response )
845- repo_err .repo_type = repo_type
846- repo_err .repo_id = repo_id
847- raise repo_err from e
835+ raise _format (RepositoryNotFoundError , message , response , repo_type = repo_type , repo_id = repo_id ) from e
848836
849837 elif response .status_code == 400 :
850838 message = (
@@ -919,7 +907,9 @@ def _warn_on_warning_headers(response: httpx.Response) -> None:
919907_HfHubHTTPErrorT = TypeVar ("_HfHubHTTPErrorT" , bound = HfHubHTTPError )
920908
921909
922- def _format (error_type : type [_HfHubHTTPErrorT ], custom_message : str , response : httpx .Response ) -> _HfHubHTTPErrorT :
910+ def _format (
911+ error_type : type [_HfHubHTTPErrorT ], custom_message : str , response : httpx .Response , ** attrs : Any
912+ ) -> _HfHubHTTPErrorT :
923913 server_errors = []
924914
925915 # Retrieve server error from header
@@ -1009,7 +999,10 @@ def _format(error_type: type[_HfHubHTTPErrorT], custom_message: str, response: h
1009999 final_error_message += request_id_message
10101000
10111001 # Return
1012- return error_type (final_error_message .strip (), response = response , server_message = server_message or None )
1002+ err = error_type (final_error_message .strip (), response = response , server_message = server_message or None )
1003+ for k , v in attrs .items ():
1004+ setattr (err , k , v )
1005+ return err
10131006
10141007
10151008def _curlify (request : httpx .Request ) -> str :
0 commit comments