Skip to content
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

Add server message to HTTPError (if any) #1015

Merged
merged 2 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/huggingface_hub/utils/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RepositoryNotFoundError(HTTPError):

```py
>>> from huggingface_hub import model_info
>>> model_info("<non_existant_repository>")
>>> model_info("<non_existent_repository>")
huggingface_hub.utils._errors.RepositoryNotFoundError: 404 Client Error: Repository Not Found for url: <url>
```
"""
Expand All @@ -30,7 +30,7 @@ class RevisionNotFoundError(HTTPError):

```py
>>> from huggingface_hub import hf_hub_download
>>> hf_hub_download('bert-base-cased', 'config.json', revision='<non-existant-revision>')
>>> hf_hub_download('bert-base-cased', 'config.json', revision='<non-existent-revision>')
huggingface_hub.utils._errors.RevisionNotFoundError: 404 Client Error: Revision Not Found for url: <url>
```
"""
Expand All @@ -48,7 +48,7 @@ class EntryNotFoundError(HTTPError):

```py
>>> from huggingface_hub import hf_hub_download
>>> hf_hub_download('bert-base-cased', '<non-existant-file>')
>>> hf_hub_download('bert-base-cased', '<non-existent-file>')
huggingface_hub.utils._errors.EntryNotFoundError: 404 Client Error: Entry Not Found for url: <url>
```
"""
Expand Down Expand Up @@ -101,6 +101,26 @@ def _add_request_id_to_error_args(e, request_id):
e.args = (e.args[0] + f" (Request ID: {request_id})",) + e.args[1:]


def _add_server_message_to_error_args(e: HTTPError, response: Response):
"""
If the server response raises an HTTPError, we try to decode the response body and
find an error message. If the server returned one, it is added to the HTTPError
message.
"""
try:
server_message = response.json().get("error", None)
except JSONDecodeError:
return

if (
server_message is not None
and len(server_message) > 0
and len(e.args) > 0
and isinstance(e.args[0], str)
):
e.args = (e.args[0] + "\n\n" + str(server_message),) + e.args[1:]


def _raise_for_status(response):
"""
Internal version of `response.raise_for_status()` that will refine a
Expand Down Expand Up @@ -144,6 +164,7 @@ def _raise_for_status(response):
e = RepositoryNotFoundError(message, response)

_add_request_id_to_error_args(e, request_id)
_add_server_message_to_error_args(e, response)

raise e

Expand Down
5 changes: 5 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,11 @@ def test_create_commit_conflict(self):
parent_commit=parent_commit,
)
self.assertEqual(exc_ctx.exception.response.status_code, 412)
self.assertIn(
# Check the server message is added to the exception
"A commit has happened since. Please refresh and try again.",
str(exc_ctx.exception),
)
except Exception as err:
self.fail(err)
finally:
Expand Down