🐛 Fix: Prevent JSONDecodeError from masking HTTP errors in non-JSON responses #2
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.
📋 Summary
Resolves #1 where
504 Gateway Timeout,502 Bad Gateway, and other infrastructure errors would throwJSONDecodeErrorinstead of showing the actual HTTP error, making debugging extremely difficult in production environments.🔍 Problem Description
When HTTP errors occurred with non-JSON responses (empty bodies, HTML error pages, plain text), the client would attempt to parse
response.json()and fail with:This masked the original HTTP errors, preventing proper error handling and monitoring.
💡 Solution
Refactored error handling in
BaseRequest._do_request()with two new helper methods following Clean Code principles:_build_error_message()- Constructs detailed error messages with proper context_extract_response_content()- Safely extracts response content with JSON/text fallback🔧 Changes Made
Code Changes:
BaseRequest._do_request()error handling_build_error_message()method with comprehensive documentation_extract_response_content()method with safe JSON parsingHISTORY.rstwith detailed changelogTest Coverage:
ErrorHandlingTestCase🔗 Related Issues
Fixes #1
📝 Testing Instructions