-
Notifications
You must be signed in to change notification settings - Fork 1k
[CLI] Better CLI errors formatting #3889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f48630d
Better CLI errors
Wauplin 9ee17c0
Fix label capitalization consistency: lowercase when mid-sentence
cursoragent 8f7196b
Fix mypy errors: make _format() generic to preserve subclass types
cursoragent 8fdb94a
Fix mypy: use distinct variable names per error branch in hf_raise_fo…
cursoragent 9911947
Merge branch 'main' into better-cli-errors
Wauplin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ | |
| from contextlib import contextmanager | ||
| from dataclasses import dataclass | ||
| from shlex import quote | ||
| from typing import Any, Callable, Generator, Mapping, Optional, Union | ||
| from typing import Any, Callable, Generator, Mapping, Optional, TypeVar, Union | ||
| from urllib.parse import urlparse | ||
|
|
||
| import httpx | ||
|
|
@@ -169,6 +169,47 @@ def parse_ratelimit_headers(headers: Mapping[str, str]) -> Optional[RateLimitInf | |
| flags=re.VERBOSE, | ||
| ) | ||
|
|
||
| # Regex to extract repo_type and repo_id from API URLs. | ||
| # Captures: group(1) = repo_type plural (models/datasets/spaces), group(2) = first path segment, group(3) = optional second segment. | ||
| _REPO_ID_FROM_URL_REGEX = re.compile(r"^https?://[^/]+/api/(models|datasets|spaces)/([^/]+)(?:/([^/]+))?") | ||
|
|
||
| # Regex to extract bucket_id (namespace/name) from bucket API URLs. | ||
| _BUCKET_ID_FROM_URL_REGEX = re.compile(r"^https?://[^/]+/api/buckets/([^/]+/[^/]+)") | ||
|
|
||
| # Sub-paths that follow a repo_id in API URLs (not part of the repo name). | ||
| _REPO_URL_SUBPATHS = {"resolve", "tree", "blob", "raw", "refs", "commit", "discussions", "settings", "revision"} | ||
|
Comment on lines
+174
to
+180
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: not 100% bullet-proof but parsing doesn't have to be perfect (it's just a convenience field for better errors) |
||
|
|
||
|
|
||
| def _parse_repo_info_from_url(url: str) -> tuple[Optional[str], Optional[str]]: | ||
| """Extract (repo_type, repo_id) from an API URL. | ||
|
|
||
| Returns canonical repo_type values: "model", "dataset", "space" (or None). | ||
|
|
||
| Examples: | ||
| >>> _parse_repo_info_from_url("https://huggingface.co/api/models/user/repo") | ||
| ("model", "user/repo") | ||
| >>> _parse_repo_info_from_url("https://huggingface.co/api/datasets/user/repo/resolve/main/data.csv") | ||
| ("dataset", "user/repo") | ||
| >>> _parse_repo_info_from_url("https://huggingface.co/api/models/bert-base-cased/resolve/main/config.json") | ||
| ("model", "bert-base-cased") | ||
| """ | ||
| match = _REPO_ID_FROM_URL_REGEX.search(url) | ||
| if not match: | ||
| return None, None | ||
| repo_type = constants.REPO_TYPES_MAPPING.get(match.group(1)) | ||
| first, second = match.group(2), match.group(3) | ||
| if second and second not in _REPO_URL_SUBPATHS: | ||
| repo_id = f"{first}/{second}" | ||
| else: | ||
| repo_id = first | ||
| return repo_type, repo_id | ||
|
|
||
|
|
||
| def _parse_bucket_id_from_url(url: str) -> Optional[str]: | ||
| """Extract bucket_id (namespace/name) from a bucket API URL.""" | ||
| match = _BUCKET_ID_FROM_URL_REGEX.search(url) | ||
| return match.group(1) if match else None | ||
|
|
||
|
|
||
| def hf_request_event_hook(request: httpx.Request) -> None: | ||
| """ | ||
|
|
@@ -725,19 +766,34 @@ def hf_raise_for_status(response: httpx.Response, endpoint_name: Optional[str] = | |
| error_code = response.headers.get("X-Error-Code") | ||
| error_message = response.headers.get("X-Error-Message") | ||
|
|
||
| # Parse repo info from request URL (used to enrich errors below) | ||
| request_url = ( | ||
| str(response.request.url) if response.request is not None and response.request.url is not None else None | ||
| ) | ||
| repo_type, repo_id = _parse_repo_info_from_url(request_url) if request_url else (None, None) | ||
|
|
||
| if error_code == "RevisionNotFound": | ||
| message = f"{response.status_code} Client Error." + "\n\n" + f"Revision Not Found for url: {response.url}." | ||
| raise _format(RevisionNotFoundError, message, response) from e | ||
| revision_err = _format(RevisionNotFoundError, message, response) | ||
| revision_err.repo_type = repo_type | ||
| revision_err.repo_id = repo_id | ||
| raise revision_err from e | ||
|
|
||
| elif error_code == "EntryNotFound": | ||
| message = f"{response.status_code} Client Error." + "\n\n" + f"Entry Not Found for url: {response.url}." | ||
| raise _format(RemoteEntryNotFoundError, message, response) from e | ||
| entry_err = _format(RemoteEntryNotFoundError, message, response) | ||
| entry_err.repo_type = repo_type | ||
| entry_err.repo_id = repo_id | ||
| raise entry_err from e | ||
|
|
||
| elif error_code == "GatedRepo": | ||
| message = ( | ||
| f"{response.status_code} Client Error." + "\n\n" + f"Cannot access gated repo for url {response.url}." | ||
| ) | ||
| raise _format(GatedRepoError, message, response) from e | ||
| gated_err = _format(GatedRepoError, message, response) | ||
| gated_err.repo_type = repo_type | ||
| gated_err.repo_id = repo_id | ||
| raise gated_err from e | ||
|
|
||
| elif error_message == "Access to this resource is disabled.": | ||
| message = ( | ||
|
|
@@ -751,9 +807,8 @@ def hf_raise_for_status(response: httpx.Response, endpoint_name: Optional[str] = | |
|
|
||
| elif ( | ||
| error_code == "RepoNotFound" | ||
| and response.request is not None | ||
| and response.request.url is not None | ||
| and BUCKET_API_REGEX.search(str(response.request.url)) is not None | ||
| and request_url is not None | ||
| and BUCKET_API_REGEX.search(request_url) is not None | ||
| ): | ||
| message = ( | ||
| f"{response.status_code} Client Error." | ||
|
|
@@ -762,14 +817,15 @@ def hf_raise_for_status(response: httpx.Response, endpoint_name: Optional[str] = | |
| + "\nPlease make sure you specified the correct bucket id (namespace/name)." | ||
| + "\nIf the bucket is private, make sure you are authenticated." | ||
| ) | ||
| raise _format(BucketNotFoundError, message, response) from e | ||
| bucket_err = _format(BucketNotFoundError, message, response) | ||
| bucket_err.bucket_id = _parse_bucket_id_from_url(request_url) | ||
| raise bucket_err from e | ||
|
|
||
| elif error_code == "RepoNotFound" or ( | ||
| response.status_code == 401 | ||
| and error_message != "Invalid credentials in Authorization header" | ||
| and response.request is not None | ||
| and response.request.url is not None | ||
| and REPO_API_REGEX.search(str(response.request.url)) is not None | ||
| and request_url is not None | ||
| and REPO_API_REGEX.search(request_url) is not None | ||
| ): | ||
| # 401 is misleading as it is returned for: | ||
| # - private and gated repos if user is not authenticated | ||
|
|
@@ -785,7 +841,10 @@ def hf_raise_for_status(response: httpx.Response, endpoint_name: Optional[str] = | |
| " make sure you are authenticated. For more details, see" | ||
| " https://huggingface.co/docs/huggingface_hub/authentication" | ||
| ) | ||
| raise _format(RepositoryNotFoundError, message, response) from e | ||
| repo_err = _format(RepositoryNotFoundError, message, response) | ||
| repo_err.repo_type = repo_type | ||
| repo_err.repo_id = repo_id | ||
| raise repo_err from e | ||
|
|
||
| elif response.status_code == 400: | ||
| message = ( | ||
|
|
@@ -857,7 +916,10 @@ def _warn_on_warning_headers(response: httpx.Response) -> None: | |
| logger.warning(message) | ||
|
|
||
|
|
||
| def _format(error_type: type[HfHubHTTPError], custom_message: str, response: httpx.Response) -> HfHubHTTPError: | ||
| _HfHubHTTPErrorT = TypeVar("_HfHubHTTPErrorT", bound=HfHubHTTPError) | ||
|
|
||
|
|
||
| def _format(error_type: type[_HfHubHTTPErrorT], custom_message: str, response: httpx.Response) -> _HfHubHTTPErrorT: | ||
| server_errors = [] | ||
|
|
||
| # Retrieve server error from header | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.