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.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(api): handle non-object JSON auth error bodies (#44470)
Eddy ZHANG309eb672e3c5
diff --git a/api/services/data_source/auth/firecrawl/firecrawl.py b/api/services/data_source/auth/firecrawl/firecrawl.pyindex d393c067284..66b2e2c2f0a 100644--- a/api/services/data_source/auth/firecrawl/firecrawl.py+++ b/api/services/data_source/auth/firecrawl/firecrawl.py@@ -63,6 +63,8 @@ class FirecrawlAuth:payload = response.json()except json.JSONDecodeError:payload = {}+ if not isinstance(payload, dict):+ payload = {}error_message = payload.get("error") or payload.get("message") or (response.text or "Unknown error occurred")raise DataSourceApiKeyAuthCredentialValidationError(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"diff --git a/api/services/data_source/auth/jina/jina.py b/api/services/data_source/auth/jina/jina.pyindex 52d1cd58bfd..2fb2a2fba56 100644--- a/api/services/data_source/auth/jina/jina.py+++ b/api/services/data_source/auth/jina/jina.py@@ -55,18 +55,16 @@ class JinaAuth:raise DataSourceApiKeyAuthProviderUnavailableError("jinareader", response.status_code)if response.status_code in {402, 409}:- try:- error_message = response.json().get("error", "Unknown error occurred")- except json.JSONDecodeError:+ error_message = _error_field(response.text)+ if error_message is None:error_message = response.text or "Unknown error occurred"raise DataSourceApiKeyAuthCredentialValidationError(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")else:if response.text:- try:- error_message = json.loads(response.text).get("error", "Unknown error occurred")- except json.JSONDecodeError:+ error_message = _error_field(response.text)+ if error_message is None:error_message = response.textraise DataSourceApiKeyAuthCredentialValidationError(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"@@ -74,3 +72,14 @@ class JinaAuth:raise DataSourceApiKeyAuthCredentialValidationError(f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}")+++def _error_field(body: str) -> str | None:+ """The `error` field of a JSON object body, or None when the body is not a JSON object."""+ try:+ payload = json.loads(body)+ except json.JSONDecodeError:+ return None+ if not isinstance(payload, dict):+ return None+ return payload.get("error", "Unknown error occurred")diff --git a/api/services/data_source/auth/watercrawl/watercrawl.py b/api/services/data_source/auth/watercrawl/watercrawl.pyindex b93b7c17eb3..97ff1af77c3 100644--- a/api/services/data_source/auth/watercrawl/watercrawl.py+++ b/api/services/data_source/auth/watercrawl/watercrawl.py@@ -53,18 +53,16 @@ class WatercrawlAuth:raise DataSourceApiKeyAuthProviderUnavailableError("watercrawl", response.status_code)if response.status_code in {402, 409}:- try:- error_message = response.json().get("error", "Unknown error occurred")- except json.JSONDecodeError:+ error_message = _error_field(response.text)+ if error_message is None:error_message = response.text or "Unknown error occurred"raise DataSourceApiKeyAuthCredentialValidationError(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}")else:if response.text:- try:- error_message = json.loads(response.text).get("error", "Unknown error occurred")- except json.JSONDecodeError:+ error_message = _error_field(response.text)+ if error_message is None:error_message = response.textraise DataSourceApiKeyAuthCredentialValidationError(f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"@@ -72,3 +70,14 @@ class WatercrawlAuth:raise DataSourceApiKeyAuthCredentialValidationError(f"Unexpected error occurred while trying to authorize. Status code: {response.status_code}")+++def _error_field(body: str) -> str | None:+ """The `error` field of a JSON object body, or None when the body is not a JSON object."""+ try:+ payload = json.loads(body)+ except json.JSONDecodeError:+ return None+ if not isinstance(payload, dict):+ return None+ return payload.get("error", "Unknown error occurred")diff --git a/api/tests/unit_tests/services/auth/test_firecrawl_auth.py b/api/tests/unit_tests/services/auth/test_firecrawl_auth.pyindex f4377f7877e..3adb27801e8 100644--- a/api/tests/unit_tests/services/auth/test_firecrawl_auth.py+++ b/api/tests/unit_tests/services/auth/test_firecrawl_auth.py@@ -153,6 +153,22 @@ class TestFirecrawlAuth:auth_instance.validate_credentials()assert str(exc_info.value) == expected_error_contains+ @pytest.mark.parametrize(+ ("status_code", "body"),+ [(402, '"Payment required"'), (402, "[]"), (401, "null"), (403, '"Forbidden"')],+ ids=["string-402", "array-402", "null-401", "string-403"],+ )+ @patch("services.data_source.auth.firecrawl.firecrawl.ssrf_proxy.post", autospec=True)+ def test_should_handle_json_error_body_that_is_not_an_object(+ self, mock_post: MagicMock, status_code: int, body: str, auth_instance: FirecrawlAuth+ ) -> None:+ """A JSON string, array or null body is reported like a non-JSON body."""+ mock_post.return_value = httpx.Response(status_code, text=body)++ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:+ auth_instance.validate_credentials()+ assert str(exc_info.value) == f"Failed to authorize. Status code: {status_code}. Error: {body}"+@pytest.mark.parametrize(("exception_type", "exception_message"),[diff --git a/api/tests/unit_tests/services/auth/test_jina_auth.py b/api/tests/unit_tests/services/auth/test_jina_auth.pyindex a610cb114e3..2c423ae6ebd 100644--- a/api/tests/unit_tests/services/auth/test_jina_auth.py+++ b/api/tests/unit_tests/services/auth/test_jina_auth.py@@ -80,6 +80,24 @@ class TestJinaAuth:auth.validate_credentials()assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"+ @pytest.mark.parametrize(+ ("status_code", "body"),+ [(402, '"Payment required"'), (402, "[]"), (401, "null"), (403, '"Forbidden"')],+ ids=["string-402", "array-402", "null-401", "string-403"],+ )+ @patch("services.data_source.auth.jina.jina._http_client.post", autospec=True)+ def test_should_handle_json_error_body_that_is_not_an_object(+ self, mock_post: MagicMock, status_code: int, body: str+ ) -> None:+ """A JSON string, array or null body is reported like a non-JSON body."""+ mock_post.return_value = httpx.Response(status_code, text=body)++ auth = JinaAuth(_credentials())++ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:+ auth.validate_credentials()+ assert str(exc_info.value) == f"Failed to authorize. Status code: {status_code}. Error: {body}"+@patch("services.data_source.auth.jina.jina._http_client.post", autospec=True)def test_should_handle_http_409_error(self, mock_post: MagicMock) -> None:"""Test handling of 409 Conflict error"""diff --git a/api/tests/unit_tests/services/auth/test_watercrawl_auth.py b/api/tests/unit_tests/services/auth/test_watercrawl_auth.pyindex 16661593f60..c1971ad2ec9 100644--- a/api/tests/unit_tests/services/auth/test_watercrawl_auth.py+++ b/api/tests/unit_tests/services/auth/test_watercrawl_auth.py@@ -127,6 +127,22 @@ class TestWatercrawlAuth:auth_instance.validate_credentials()assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"+ @pytest.mark.parametrize(+ ("status_code", "body"),+ [(402, '"Payment required"'), (402, "[]"), (401, "null"), (403, '"Forbidden"')],+ ids=["string-402", "array-402", "null-401", "string-403"],+ )+ @patch("services.data_source.auth.watercrawl.watercrawl.ssrf_proxy.get", autospec=True)+ def test_should_handle_json_error_body_that_is_not_an_object(+ self, mock_get: MagicMock, status_code: int, body: str, auth_instance: WatercrawlAuth+ ) -> None:+ """A JSON string, array or null body is reported like a non-JSON body."""+ mock_get.return_value = httpx.Response(status_code, text=body)++ with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:+ auth_instance.validate_credentials()+ assert str(exc_info.value) == f"Failed to authorize. Status code: {status_code}. Error: {body}"+@pytest.mark.parametrize(("status_code", "response_text", "expected_error_contains"),[