From 388177428286e842785cd7488567cce70f28dd56 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 25 Aug 2022 16:21:47 +0200 Subject: [PATCH 1/2] hotfix to add server message to HTTPError if any --- src/huggingface_hub/utils/_errors.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/huggingface_hub/utils/_errors.py b/src/huggingface_hub/utils/_errors.py index e2f01b7252..19cc6820d9 100644 --- a/src/huggingface_hub/utils/_errors.py +++ b/src/huggingface_hub/utils/_errors.py @@ -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 @@ -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 From 55a05a962b7e2560c47138d62623b60998906f54 Mon Sep 17 00:00:00 2001 From: Wauplin Date: Thu, 25 Aug 2022 17:12:58 +0200 Subject: [PATCH 2/2] add test to check added response --- src/huggingface_hub/utils/_errors.py | 6 +++--- tests/test_hf_api.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/huggingface_hub/utils/_errors.py b/src/huggingface_hub/utils/_errors.py index 19cc6820d9..1d6cbe922f 100644 --- a/src/huggingface_hub/utils/_errors.py +++ b/src/huggingface_hub/utils/_errors.py @@ -12,7 +12,7 @@ class RepositoryNotFoundError(HTTPError): ```py >>> from huggingface_hub import model_info - >>> model_info("") + >>> model_info("") huggingface_hub.utils._errors.RepositoryNotFoundError: 404 Client Error: Repository Not Found for url: ``` """ @@ -30,7 +30,7 @@ class RevisionNotFoundError(HTTPError): ```py >>> from huggingface_hub import hf_hub_download - >>> hf_hub_download('bert-base-cased', 'config.json', revision='') + >>> hf_hub_download('bert-base-cased', 'config.json', revision='') huggingface_hub.utils._errors.RevisionNotFoundError: 404 Client Error: Revision Not Found for url: ``` """ @@ -48,7 +48,7 @@ class EntryNotFoundError(HTTPError): ```py >>> from huggingface_hub import hf_hub_download - >>> hf_hub_download('bert-base-cased', '') + >>> hf_hub_download('bert-base-cased', '') huggingface_hub.utils._errors.EntryNotFoundError: 404 Client Error: Entry Not Found for url: ``` """ diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 2191fc09b2..5c6c57625a 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -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: