Repository: huggingface/huggingface_hub
Pull request #1115 (2022-10-14 08:12:39)

Propagate error messages when multiple on BadRequest
Fix https://github.com/huggingface/huggingface_hub/issues/1114.

@Narsil exception will now be raised as such:

```
huggingface_hub.utils._errors.BadRequestError:  (Request ID: A83RPh8Orl5DA0S92qTVX)

Bad request for commit endpoint:
You can't have a key with a "." or "$" character in your YAML metadata
```

HuggingFaceDocBuilderDev: The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/huggingface_hub/pr_1115). All of your documentation changes will be reflected on that endpoint.
codecov[bot]: # [Codecov](https://codecov.io/gh/huggingface/huggingface_hub/pull/1115?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface) Report
Base: **84.99**% // Head: **85.00**% // Increases project coverage by **`+0.01%`** :tada:
> Coverage data is based on head [(`041906d`)](https://codecov.io/gh/huggingface/huggingface_hub/pull/1115?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface) compared to base [(`e2fe217`)](https://codecov.io/gh/huggingface/huggingface_hub/commit/e2fe217c957feb2f1b8b0792b4b55b48435e3431?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1115      +/-   ##
==========================================
+ Coverage   84.99%   85.00%   +0.01%     
==========================================
  Files          39       39              
  Lines        3951     3955       +4     
==========================================
+ Hits         3358     3362       +4     
  Misses        593      593              
```


| [Impacted Files](https://codecov.io/gh/huggingface/huggingface_hub/pull/1115?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface) | Coverage Δ | |
|---|---|---|
| [src/huggingface\_hub/utils/\_errors.py](https://codecov.io/gh/huggingface/huggingface_hub/pull/1115/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface#diff-c3JjL2h1Z2dpbmdmYWNlX2h1Yi91dGlscy9fZXJyb3JzLnB5) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/huggingface/huggingface_hub/pull/1115?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface).


Patch:
diff --git a/src/huggingface_hub/utils/_errors.py b/src/huggingface_hub/utils/_errors.py
index f9e46a85..7fd03612 100644
--- a/src/huggingface_hub/utils/_errors.py
+++ b/src/huggingface_hub/utils/_errors.py
@@ -43,9 +43,23 @@ class HfHubHTTPError(HTTPError):
         if response is not None:
             self.request_id = response.headers.get("X-Request-Id")
             try:
-                self.server_message = response.json().get("error", None)
+                server_data = response.json()
             except JSONDecodeError:
-                pass
+                server_data = {}
+
+            self.server_message = server_data.get("error", None)
+
+            msg_from_errors = "\n".join(
+                error["message"]
+                for error in server_data.get("errors", [])
+                if "message" in error
+            )
+            if msg_from_errors != "":
+                self.server_message = (
+                    self.server_message + "\n" + msg_from_errors
+                    if self.server_message is not None
+                    else msg_from_errors
+                )
 
         super().__init__(
             _format_error_message(


Test patch:
diff --git a/tests/test_utils_cache.py b/tests/test_utils_cache.py
index 88a0957a..b922525a 100644
--- a/tests/test_utils_cache.py
+++ b/tests/test_utils_cache.py
@@ -459,15 +459,15 @@ class TestCorruptedCacheUtils(unittest.TestCase):
         # Readme.md has been modified and then accessed more recently
         self.assertGreaterEqual(
             readme_file_2.blob_last_modified - readme_file_1.blob_last_modified,
-            TIME_GAP * 0.999,  # 0.999 factor because not exactly precise
+            TIME_GAP * 0.9,  # 0.9 factor because not exactly precise
         )
         self.assertGreaterEqual(
             readme_file_2.blob_last_accessed - readme_file_1.blob_last_accessed,
-            2 * TIME_GAP * 0.999,  # 0.999 factor because not exactly precise
+            2 * TIME_GAP * 0.9,  # 0.9 factor because not exactly precise
         )
         self.assertGreaterEqual(
             readme_file_2.blob_last_accessed - readme_file_2.blob_last_modified,
-            TIME_GAP * 0.999,  # 0.999 factor because not exactly precise
+            TIME_GAP * 0.9,  # 0.9 factor because not exactly precise
         )
 
         # Comparison of last_accessed/last_modified between file and repo
diff --git a/tests/test_utils_errors.py b/tests/test_utils_errors.py
index 52fa0268..fc6f9a58 100644
--- a/tests/test_utils_errors.py
+++ b/tests/test_utils_errors.py
@@ -204,6 +204,25 @@ class TestHfHubHTTPError(unittest.TestCase):
             " server",
         )
 
+    def test_hf_hub_http_error_init_with_multiple_server_errors(
+        self,
+    ) -> None:
+        """Test server errors are added to the error message after the details.
+
+        Regression test for https://github.com/huggingface/huggingface_hub/issues/1114.
+        """
+        self.response._content = (
+            b'{"httpStatusCode": 400, "errors": [{"message": "this is error 1", "type":'
+            b' "error"}, {"message": "this is error 2", "type": "error"}]}'
+        )
+        error = HfHubHTTPError(
+            "this is a message\n\nSome details.", response=self.response
+        )
+        self.assertEqual(
+            str(error),
+            "this is a message\n\nSome details.\nthis is error 1\nthis is error 2",
+        )
+
     def test_hf_hub_http_error_init_with_server_error_already_in_message(
         self,
     ) -> None: