Skip to content

Commit

Permalink
Add server message to HTTPError (if any) (#1015)
Browse files Browse the repository at this point in the history
* hotfix to add server message to HTTPError if any

* add test to check added response
  • Loading branch information
Wauplin committed Aug 25, 2022
1 parent 13da627 commit e447685
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
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

0 comments on commit e447685

Please sign in to comment.