From 435061769418705a1aba166de9b7dfd09dd2dab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=BD=E5=B0=98?= Date: Tue, 16 Jun 2026 16:16:30 +0800 Subject: [PATCH] fix(watercrawl): handle non-json auth errors (#37498) --- api/services/auth/watercrawl/watercrawl.py | 10 ++++++++-- .../services/auth/test_watercrawl_auth.py | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/api/services/auth/watercrawl/watercrawl.py b/api/services/auth/watercrawl/watercrawl.py index d07c2cc318..ac8ec24d89 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 1d561731d4..6d76d04687 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)