Skip to content

fix: handle non-json and malformed HTTP error responses safely - #147

Open
jdabid wants to merge 1 commit into
fintoc-com:masterfrom
jdabid:fix/safe-fintoc-error-handling
Open

jdabid wants to merge 1 commit into
fintoc-com:masterfrom
jdabid:fix/safe-fintoc-error-handling

Conversation

@jdabid

@jdabid jdabid commented Nov 22, 2026

Copy link
Copy Markdown

Summary

Hardens HTTP status error handling in fintoc_error_from and FintocError against non-JSON (HTML 502/503/504), empty, or malformed error responses from upstream gateways, and adds comprehensive unit test coverage.

Details

  • fintoc/utils.py:
    • Catches JSONDecodeError and AttributeError when decoding error payloads from upstream proxies or maintenance pages (e.g., Cloudflare 502/503 HTML).
    • Falls back to constructing a clean ApiError with the status code and available response text instead of raising an uncaught JSONDecodeError or KeyError.
    • Safely extracts error details from flat error dictionaries (e.g. {"detail": ...} or {"message": ...}) and nested error objects without a type field.
  • fintoc/errors.py:
    • Guarded FintocError.__init__ against TypeError when type is missing or message is None.
  • tests/test_errors.py:
    • Added new unit test suite testing initialization of FintocError and its subclasses under various payloads.
  • tests/test_utils.py:
    • Added TestFintocErrorFrom test suite covering standard errors, HTML 502 Bad Gateway, empty 500 responses, flat error dicts, and untyped error objects.

Release notes

  • error: Safely handle non-JSON and malformed HTTP error responses by raising a clean ApiError.

Comment thread fintoc/utils.py
"message": str(flat_message),
}
)
except (JSONDecodeError, AttributeError):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Mid risk — Un body con bytes no UTF-8 se salta el fallback a ApiError

Cuando una llamada normal del SDK recibe un 502 con una página HTML en Windows-1252 que incluye texto acentuado, response.json() en httpx 0.28 (permitido por el SDK) pasa los bytes directo al decoder JSON, que lanza UnicodeDecodeError antes de parsear. Esa excepción no se captura, así que la llamada falla igual que en master y una integración que captura FintocError no puede manejar el 502 por esa vía.

Ver sugerencia

Fix: Captura también UnicodeDecodeError para que el fallback use response.text y devuelva un ApiError con el status HTTP.

Suggested change
except (JSONDecodeError, AttributeError):
except (JSONDecodeError, UnicodeDecodeError, AttributeError):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant