Skip to content

Commit e447685

Browse files
committed
Add server message to HTTPError (if any) (#1015)
* hotfix to add server message to HTTPError if any * add test to check added response
1 parent 13da627 commit e447685

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/huggingface_hub/utils/_errors.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RepositoryNotFoundError(HTTPError):
1212
1313
```py
1414
>>> from huggingface_hub import model_info
15-
>>> model_info("<non_existant_repository>")
15+
>>> model_info("<non_existent_repository>")
1616
huggingface_hub.utils._errors.RepositoryNotFoundError: 404 Client Error: Repository Not Found for url: <url>
1717
```
1818
"""
@@ -30,7 +30,7 @@ class RevisionNotFoundError(HTTPError):
3030
3131
```py
3232
>>> from huggingface_hub import hf_hub_download
33-
>>> hf_hub_download('bert-base-cased', 'config.json', revision='<non-existant-revision>')
33+
>>> hf_hub_download('bert-base-cased', 'config.json', revision='<non-existent-revision>')
3434
huggingface_hub.utils._errors.RevisionNotFoundError: 404 Client Error: Revision Not Found for url: <url>
3535
```
3636
"""
@@ -48,7 +48,7 @@ class EntryNotFoundError(HTTPError):
4848
4949
```py
5050
>>> from huggingface_hub import hf_hub_download
51-
>>> hf_hub_download('bert-base-cased', '<non-existant-file>')
51+
>>> hf_hub_download('bert-base-cased', '<non-existent-file>')
5252
huggingface_hub.utils._errors.EntryNotFoundError: 404 Client Error: Entry Not Found for url: <url>
5353
```
5454
"""
@@ -101,6 +101,26 @@ def _add_request_id_to_error_args(e, request_id):
101101
e.args = (e.args[0] + f" (Request ID: {request_id})",) + e.args[1:]
102102

103103

104+
def _add_server_message_to_error_args(e: HTTPError, response: Response):
105+
"""
106+
If the server response raises an HTTPError, we try to decode the response body and
107+
find an error message. If the server returned one, it is added to the HTTPError
108+
message.
109+
"""
110+
try:
111+
server_message = response.json().get("error", None)
112+
except JSONDecodeError:
113+
return
114+
115+
if (
116+
server_message is not None
117+
and len(server_message) > 0
118+
and len(e.args) > 0
119+
and isinstance(e.args[0], str)
120+
):
121+
e.args = (e.args[0] + "\n\n" + str(server_message),) + e.args[1:]
122+
123+
104124
def _raise_for_status(response):
105125
"""
106126
Internal version of `response.raise_for_status()` that will refine a
@@ -144,6 +164,7 @@ def _raise_for_status(response):
144164
e = RepositoryNotFoundError(message, response)
145165

146166
_add_request_id_to_error_args(e, request_id)
167+
_add_server_message_to_error_args(e, response)
147168

148169
raise e
149170

tests/test_hf_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,11 @@ def test_create_commit_conflict(self):
882882
parent_commit=parent_commit,
883883
)
884884
self.assertEqual(exc_ctx.exception.response.status_code, 412)
885+
self.assertIn(
886+
# Check the server message is added to the exception
887+
"A commit has happened since. Please refresh and try again.",
888+
str(exc_ctx.exception),
889+
)
885890
except Exception as err:
886891
self.fail(err)
887892
finally:

0 commit comments

Comments
 (0)