Skip to content

Conversation

@ivanjun10r
Copy link
Owner

📋 Summary

Resolves #1 where 504 Gateway Timeout, 502 Bad Gateway, and other infrastructure errors would throw JSONDecodeError instead 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:

requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

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:

  • ✅ Refactored BaseRequest._do_request() error handling
  • ✅ Added _build_error_message() method with comprehensive documentation
  • ✅ Added _extract_response_content() method with safe JSON parsing
  • ✅ Maintained backward compatibility with existing JSON error responses
  • ✅ Updated HISTORY.rst with detailed changelog

Test Coverage:

  • ✅ Added 10 comprehensive test cases in ErrorHandlingTestCase
  • ✅ Covered all error scenarios: 504, 502, empty responses, HTML pages
  • ✅ Integration tests for end-to-end error handling
  • ✅ Regression tests to ensure JSON responses still work

🔗 Related Issues

Fixes #1

📝 Testing Instructions

# Clone and setup
git checkout bugfix/1-json-decode-error
pyenv activate api_pgd_env

# Run specific error handling tests
python -m pytest tests/test_client.py::ErrorHandlingTestCase -v

# Run all tests to ensure no regressions
python -m pytest tests/test_client.py -v

Refactor BaseRequest._do_request() to handle non-JSON error responses:
- Extract _build_error_message() and _extract_response_content()
- Fix 504 Gateway Timeout masking issue
- Add fallback to response.text for HTML/empty responses
- Add 10 comprehensive test cases for error scenarios

Resolves: #1
@codecov-commenter
Copy link

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment

Thanks for integrating Codecov - We've got you covered ☂️

@ivanjun10r ivanjun10r merged commit 9887345 into main Oct 16, 2025
2 checks passed
@ivanjun10r ivanjun10r deleted the bugfix/1-json-decode-error branch October 16, 2025 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] JSONDecodeError em respostas HTTP de erro não-JSON (ex: 504 Gateway Timeout)

3 participants