mirror of
https://github.com/langgenius/dify.git
synced 2026-08-29 03:45:08 +08:00
fix(extension): chain original exception in APIBasedExtensionRequestor per PEP 3134 (#40810)
Co-authored-by: Harsh Kashyap <Harsh23Kashyap@users.noreply.github.com>
This commit is contained in:
@@ -41,10 +41,10 @@ class APIBasedExtensionRequestor:
|
||||
json={"point": point.value, "params": params},
|
||||
headers=headers,
|
||||
)
|
||||
except httpx.TimeoutException:
|
||||
raise ValueError("request timeout")
|
||||
except httpx.RequestError:
|
||||
raise ValueError("request connection error")
|
||||
except httpx.TimeoutException as e:
|
||||
raise ValueError("request timeout") from e
|
||||
except httpx.RequestError as e:
|
||||
raise ValueError("request connection error") from e
|
||||
|
||||
if response.status_code != 200:
|
||||
raise ValueError(f"request error, status_code: {response.status_code}, content: {response.text[:100]}")
|
||||
|
||||
@@ -89,23 +89,29 @@ def test_request_timeout(mocker: MockerFixture):
|
||||
mock_client = mocker.MagicMock()
|
||||
mock_client_instance = mock_client.__enter__.return_value
|
||||
mocker.patch("httpx.Client", return_value=mock_client)
|
||||
mock_client_instance.request.side_effect = httpx.TimeoutException("timeout")
|
||||
original = httpx.TimeoutException("timeout")
|
||||
mock_client_instance.request.side_effect = original
|
||||
|
||||
requestor = APIBasedExtensionRequestor(api_endpoint="http://example.com", api_key="test_key")
|
||||
with pytest.raises(ValueError, match="request timeout"):
|
||||
with pytest.raises(ValueError, match="request timeout") as exc_info:
|
||||
requestor.request(APIBasedExtensionPoint.PING, {})
|
||||
|
||||
assert exc_info.value.__cause__ is original
|
||||
|
||||
|
||||
def test_request_connection_error(mocker: MockerFixture):
|
||||
mock_client = mocker.MagicMock()
|
||||
mock_client_instance = mock_client.__enter__.return_value
|
||||
mocker.patch("httpx.Client", return_value=mock_client)
|
||||
mock_client_instance.request.side_effect = httpx.RequestError("error")
|
||||
original = httpx.RequestError("error")
|
||||
mock_client_instance.request.side_effect = original
|
||||
|
||||
requestor = APIBasedExtensionRequestor(api_endpoint="http://example.com", api_key="test_key")
|
||||
with pytest.raises(ValueError, match="request connection error"):
|
||||
with pytest.raises(ValueError, match="request connection error") as exc_info:
|
||||
requestor.request(APIBasedExtensionPoint.PING, {})
|
||||
|
||||
assert exc_info.value.__cause__ is original
|
||||
|
||||
|
||||
def test_request_error_status_code(mocker: MockerFixture):
|
||||
mock_client = mocker.MagicMock()
|
||||
|
||||
Reference in New Issue
Block a user