Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(watercrawl): handle non-json auth errors (#37498)

落尘435061769418
diff --git a/api/services/auth/watercrawl/watercrawl.py b/api/services/auth/watercrawl/watercrawl.py
index d07c2cc3189..ac8ec24d893 100644
--- a/api/services/auth/watercrawl/watercrawl.py
+++ b/api/services/auth/watercrawl/watercrawl.py
@@ -37,10 +37,16 @@ class WatercrawlAuth(ApiKeyAuthBase):
def _handle_error(self, response):
if response.status_code in {402, 409, 500}:
- error_message = response.json().get("error", "Unknown error occurred")
+ try:
+ error_message = response.json().get("error", "Unknown error occurred")
+ except ValueError:
+ error_message = response.text or "Unknown error occurred"
raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
else:
if response.text:
- error_message = json.loads(response.text).get("error", "Unknown error occurred")
+ try:
+ error_message = json.loads(response.text).get("error", "Unknown error occurred")
+ except ValueError:
+ error_message = response.text
raise Exception(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")
raise Exception(f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}")
diff --git a/api/tests/unit_tests/services/auth/test_watercrawl_auth.py b/api/tests/unit_tests/services/auth/test_watercrawl_auth.py
index 1d561731d4c..6d76d046874 100644
--- a/api/tests/unit_tests/services/auth/test_watercrawl_auth.py
+++ b/api/tests/unit_tests/services/auth/test_watercrawl_auth.py
@@ -99,12 +99,25 @@ class TestWatercrawlAuth:
auth_instance.validate_credentials()
assert str(exc_info.value) == f"Failed to authorize. Status code: {status_code}. Error: {error_message}"
+ @patch("services.auth.watercrawl.watercrawl.httpx.get", autospec=True)
+ def test_should_handle_http_error_with_non_json_text_response(self, mock_get, auth_instance):
+ """Test handling of known HTTP errors with non-JSON text response."""
+ mock_response = MagicMock()
+ mock_response.status_code = 402
+ mock_response.text = "Payment required"
+ mock_response.json.side_effect = ValueError("Not JSON")
+ mock_get.return_value = mock_response
+
+ with pytest.raises(Exception) as exc_info:
+ auth_instance.validate_credentials()
+ assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"
+
@pytest.mark.parametrize(
("status_code", "response_text", "has_json_error", "expected_error_contains"),
[
(403, '{"error": "Forbidden"}', True, "Failed to authorize. Status code: 403. Error: Forbidden"),
(404, "", True, "Unexpected error occurred while trying to authorize. Status code: 404"),
- (401, "Not JSON", True, "Expecting value"), # JSON decode error
+ (401, "Not JSON", True, "Failed to authorize. Status code: 401. Error: Not JSON"),
],
)
@patch("services.auth.watercrawl.watercrawl.httpx.get", autospec=True)