Skip to content

Commit 644d94a

Browse files
Fix HfHubHTTPError reduce error by adding factory function (#3579)
* fixed and added unit test Signed-off-by: You-Cheng Lin <[email protected]> * Apply style fixes --------- Signed-off-by: You-Cheng Lin <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 82f195d commit 644d94a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/huggingface_hub/errors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ def append_to_message(self, additional_message: str) -> None:
8484
"""Append additional information to the `HfHubHTTPError` initial message."""
8585
self.args = (self.args[0] + additional_message,) + self.args[1:]
8686

87+
@classmethod
88+
def _reconstruct_hf_hub_http_error(
89+
cls, message: str, response: Response, server_message: Optional[str]
90+
) -> "HfHubHTTPError":
91+
return cls(message, response=response, server_message=server_message)
92+
8793
def __reduce_ex__(self, protocol):
8894
"""Fix pickling of Exception subclass with kwargs. We need to override __reduce_ex__ of the parent class"""
89-
return (self.__class__, (str(self),), {"response": self.response, "server_message": self.server_message})
95+
return (self.__class__._reconstruct_hf_hub_http_error, (str(self), self.response, self.server_message))
9096

9197

9298
# INFERENCE CLIENT ERRORS

tests/test_utils_errors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,17 @@ def test_hf_hub_http_error_with_request_id_and_amzn_trace_id(self) -> None:
269269
assert str(error) == "this is a message (Request ID: test-id)"
270270
assert error.request_id == "test-id"
271271

272+
def test_hf_hub_error_reconstruction(self) -> None:
273+
"""Test HfHubHTTPError is reconstructed properly."""
274+
from copy import deepcopy
275+
276+
mock_response = Response(status_code=404, request=Request(method="GET", url="https://huggingface.co/fake"))
277+
error = HfHubHTTPError("this is a message", response=mock_response)
278+
copy_error = deepcopy(error)
279+
assert str(copy_error) == str(error)
280+
assert copy_error.request_id == error.request_id
281+
assert copy_error.server_message == error.server_message
282+
272283

273284
@pytest.mark.parametrize(
274285
("url", "should_match"),

0 commit comments

Comments
 (0)