-
Notifications
You must be signed in to change notification settings - Fork 3
# Fix: Occasional 502 Internal Server Error Returning Raw HTML via Python SDK #1923 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Hellnight2005
wants to merge
3
commits into
lingodotdev:main
from
Hellnight2005:fix/python-sdk-502-html-response
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import pytest | ||
| import json | ||
| from unittest.mock import Mock, patch | ||
| from lingodotdev import LingoDotDevEngine | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_502_html_handling(): | ||
| """Test that 502 errors with HTML bodies are sanitized""" | ||
| config = {"api_key": "test_key", "api_url": "https://api.test.com"} | ||
|
|
||
| html_body = "<html><body>" + ("<h1>502 Bad Gateway</h1>" * 50) + "</body></html>" | ||
| assert len(html_body) > 200 # Ensure it triggers truncation | ||
|
|
||
| with patch("lingodotdev.engine.httpx.AsyncClient.post") as mock_post: | ||
| mock_response = Mock() | ||
| mock_response.is_success = False | ||
| mock_response.status_code = 502 | ||
| mock_response.reason_phrase = "Bad Gateway" | ||
| mock_response.text = html_body | ||
| mock_response.json.side_effect = ValueError( | ||
| "Not JSON" | ||
| ) # simulating non-JSON response | ||
| mock_post.return_value = mock_response | ||
|
|
||
| async with LingoDotDevEngine(config) as engine: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| await engine.localize_text("hello", {"target_locale": "es"}) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
|
|
||
| # Assertions | ||
| assert "Server error (502): Bad Gateway." in error_msg | ||
| assert "This may be due to temporary service issues." in error_msg | ||
| assert "Response:" not in error_msg | ||
| assert "<html>" not in error_msg | ||
| assert "<body>" not in error_msg | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_500_json_handling(): | ||
| """Test that 500 errors with JSON bodies are preserved""" | ||
| config = {"api_key": "test_key", "api_url": "https://api.test.com"} | ||
| error_json = {"error": "Specific internal error message"} | ||
|
|
||
| with patch("lingodotdev.engine.httpx.AsyncClient.post") as mock_post: | ||
| mock_response = Mock() | ||
| mock_response.is_success = False | ||
| mock_response.status_code = 500 | ||
| mock_response.reason_phrase = "Internal Server Error" | ||
| mock_response.text = json.dumps(error_json) # Needed for response_preview | ||
| mock_response.json.return_value = error_json | ||
| mock_post.return_value = mock_response | ||
|
|
||
| async with LingoDotDevEngine(config) as engine: | ||
| with pytest.raises(RuntimeError) as exc_info: | ||
| await engine.localize_text("hello", {"target_locale": "es"}) | ||
|
|
||
| error_msg = str(exc_info.value) | ||
|
|
||
| # Assertions | ||
| assert "Server error (500): Internal Server Error." in error_msg | ||
| assert "Specific internal error message" in error_msg |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.