Skip to content

Commit 1371ad9

Browse files
authored
AQUA. Enhance error message generated by HF. (#864)
2 parents 37a46a8 + da3d013 commit 1371ad9

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

ads/aqua/extension/model_handler.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55

66
import re
7-
from typing import Optional
7+
from typing import Optional, Tuple
88
from urllib.parse import urlparse
99

1010
from 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:

tests/unitary/with_extras/aqua/test_model_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from notebook.base.handlers import IPythonHandler
1414
from tornado.web import HTTPError
1515

16+
from ads.aqua.common.errors import AquaRuntimeError
1617
from ads.aqua.extension.model_handler import (
1718
AquaHuggingFaceHandler,
1819
AquaModelHandler,
@@ -179,7 +180,7 @@ def test_post_negative(self, mock_uuid):
179180
return_value={"model_id": "test_model_id"}
180181
)
181182
self.mock_handler._format_custom_error_message = MagicMock(
182-
return_value="test error message"
183+
side_effect=AquaRuntimeError("test error message")
183184
)
184185
with patch.object(HfApi, "model_info") as mock_model_info:
185186
mock_model_info.side_effect = GatedRepoError(message="test message")

0 commit comments

Comments
 (0)