44# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55
66import re
7- from typing import Optional
7+ from typing import Optional , Tuple
88from urllib .parse import urlparse
99
1010from huggingface_hub import HfApi
@@ -101,48 +101,55 @@ def _find_matching_aqua_model(self, model_id: str) -> Optional[AquaModelSummary]
101101
102102 return None
103103
104- def _format_custom_error_message (self , error : HfHubHTTPError ):
104+ def _format_custom_error_message (self , error : HfHubHTTPError ) -> AquaRuntimeError :
105105 """
106106 Formats a custom error message based on the Hugging Face error response.
107107
108108 Parameters
109109 ----------
110110 error (HfHubHTTPError): The caught exception.
111111
112- Returns
113- -------
114- str : A user-friendly error message.
112+ Raises
113+ ------
114+ AquaRuntimeError : A user-friendly error message.
115115 """
116116 # Extract the repository URL from the error message if present
117117 match = re .search (r"(https://huggingface.co/[^\s]+)" , str (error ))
118118 url = match .group (1 ) if match else "the requested Hugging Face URL."
119119
120120 if isinstance (error , RepositoryNotFoundError ):
121- return (
122- f"Failed to access { url } "
123- "If the repo is private, make sure you are authenticated."
121+ raise AquaRuntimeError (
122+ reason = f"Failed to access `{ url } `. Please check if the provided repository name is correct. "
123+ "If the repo is private, make sure you are authenticated and have a valid HF token registered. "
124+ "To register your token, run this command in your terminal: `huggingface-cli login`" ,
125+ service_payload = {"error" : "RepositoryNotFoundError" },
124126 )
125- elif isinstance (error , GatedRepoError ):
126- return (
127- f"Access denied to { url } "
127+
128+ if isinstance (error , GatedRepoError ):
129+ raise AquaRuntimeError (
130+ reason = f"Access denied to `{ url } ` "
128131 "This repository is gated. Access is restricted to authorized users. "
129132 "Please request access or check with the repository administrator. "
130133 "If you are trying to access a gated repository, ensure you have a valid HF token registered. "
131- "To register your token, run this command in your terminal: `huggingface-cli login`"
132- )
133- elif isinstance (error , RevisionNotFoundError ):
134- return (
135- f"The specified revision could not be found at { url } "
136- "Please check the revision identifier and try again."
134+ "To register your token, run this command in your terminal: `huggingface-cli login`" ,
135+ service_payload = {"error" : "GatedRepoError" },
137136 )
138- else :
139- return (
140- f"An error occurred while accessing { url } "
141- "Please check your network connection and try again. "
142- "If you are trying to access a gated repository, ensure you have a valid HF token registered. "
143- "To register your token, run this command in your terminal: `huggingface-cli login`"
137+
138+ if isinstance ( error , RevisionNotFoundError ):
139+ raise AquaRuntimeError (
140+ reason = f"The specified revision could not be found at ` { url } ` "
141+ "Please check the revision identifier and try again." ,
142+ service_payload = { "error" : "RevisionNotFoundError" },
144143 )
145144
145+ raise AquaRuntimeError (
146+ reason = f"An error occurred while accessing `{ url } ` "
147+ "Please check your network connection and try again. "
148+ "If you are trying to access a gated repository, ensure you have a valid HF token registered. "
149+ "To register your token, run this command in your terminal: `huggingface-cli login`" ,
150+ service_payload = {"error" : "Error" },
151+ )
152+
146153 @handle_exceptions
147154 def post (self , * args , ** kwargs ):
148155 """Handles post request for the HF Models APIs
@@ -166,11 +173,10 @@ def post(self, *args, **kwargs):
166173 raise HTTPError (400 , Errors .MISSING_REQUIRED_PARAMETER .format ("model_id" ))
167174
168175 # Get model info from the HF
169-
170176 try :
171177 hf_model_info = HfApi ().model_info (model_id )
172178 except HfHubHTTPError as err :
173- raise AquaRuntimeError ( self ._format_custom_error_message (err ) )
179+ raise self ._format_custom_error_message (err )
174180
175181 # Check if model is not disabled
176182 if hf_model_info .disabled :
0 commit comments