fix(jina): handle non-json auth errors (#37502)

This commit is contained in:
落尘 2026-06-16 16:16:34 +08:00 committed by GitHub
parent 4350617694
commit 28cc739a93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 69 additions and 4 deletions

View File

@ -43,10 +43,16 @@ class JinaAuth(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}")

View File

@ -43,10 +43,16 @@ class JinaAuth(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}")

View File

@ -68,6 +68,22 @@ class TestJinaAuth:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"
@patch("services.auth.jina.jina._http_client.post", autospec=True)
def test_should_handle_http_error_with_non_json_text_response(self, mock_post):
"""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_post.return_value = mock_response
credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
auth = JinaAuth(credentials)
with pytest.raises(Exception) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 402. Error: Payment required"
@patch("services.auth.jina.jina._http_client.post", autospec=True)
def test_should_handle_http_409_error(self, mock_post):
"""Test handling of 409 Conflict error"""
@ -114,6 +130,22 @@ class TestJinaAuth:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 403. Error: Forbidden"
@patch("services.auth.jina.jina._http_client.post", autospec=True)
def test_should_handle_unexpected_error_with_non_json_text_response(self, mock_post):
"""Test handling of unexpected errors with non-JSON text response."""
mock_response = MagicMock()
mock_response.status_code = 403
mock_response.text = "Forbidden"
mock_response.json.side_effect = Exception("Not JSON")
mock_post.return_value = mock_response
credentials = {"auth_type": "bearer", "config": {"api_key": "test_api_key_123"}}
auth = JinaAuth(credentials)
with pytest.raises(Exception) as exc_info:
auth.validate_credentials()
assert str(exc_info.value) == "Failed to authorize. Status code: 403. Error: Forbidden"
@patch("services.auth.jina.jina._http_client.post", autospec=True)
def test_should_handle_unexpected_error_without_text(self, mock_post):
"""Test handling of unexpected errors without text response"""

View File

@ -118,6 +118,17 @@ def test_handle_error_statuses_default_unknown_error(jina_module: ModuleType) ->
auth._handle_error(response)
def test_handle_error_statuses_fall_back_to_text_body(jina_module: ModuleType) -> None:
auth = jina_module.JinaAuth(_credentials(api_key="k"))
response = MagicMock()
response.status_code = 402
response.text = "Payment required"
response.json.side_effect = ValueError("Not JSON")
with pytest.raises(Exception, match="Status code: 402.*Payment required"):
auth._handle_error(response)
def test_handle_error_with_text_json_body(jina_module: ModuleType) -> None:
auth = jina_module.JinaAuth(_credentials(api_key="k"))
response = MagicMock()
@ -128,6 +139,16 @@ def test_handle_error_with_text_json_body(jina_module: ModuleType) -> None:
auth._handle_error(response)
def test_handle_error_with_non_json_text_body(jina_module: ModuleType) -> None:
auth = jina_module.JinaAuth(_credentials(api_key="k"))
response = MagicMock()
response.status_code = 403
response.text = "Forbidden"
with pytest.raises(Exception, match="Status code: 403.*Forbidden"):
auth._handle_error(response)
def test_handle_error_with_text_json_body_missing_error(jina_module: ModuleType) -> None:
auth = jina_module.JinaAuth(_credentials(api_key="k"))
response = MagicMock()