Skip to content

fix: catch UnicodeDecodeError in _handle_error_response#171

Merged
Kamilbenkirane merged 1 commit into
mainfrom
fix/handle-binary-error-bodies-170
Feb 23, 2026
Merged

fix: catch UnicodeDecodeError in _handle_error_response#171
Kamilbenkirane merged 1 commit into
mainfrom
fix/handle-binary-error-bodies-170

Conversation

@Kamilbenkirane

Copy link
Copy Markdown
Member

Closes #170

Summary

  • _handle_error_response now catches UnicodeDecodeError from response.json() on binary error bodies
  • Adds parametrized unit tests covering three binary payload patterns (0xff, UTF-32-LE BOM, high bytes)

Root cause

json.loads() performs BOM detection on raw bytes. When a provider returns a binary error body (e.g. b'\xff ...'), json.loads() raises UnicodeDecodeError — not JSONDecodeError — which was not in the except tuple, causing it to escape instead of the expected httpx.HTTPStatusError.

Test plan

  • All existing unit tests pass
  • New TestHandleErrorResponse tests verify httpx.HTTPStatusError is raised for binary bodies
  • Pre-commit hooks pass (ruff, mypy, bandit)

json.loads() raises UnicodeDecodeError (not JSONDecodeError) on binary
bodies with BOM-like bytes (e.g. 0xff). Add it to the except tuple so
the method falls through to the response.text fallback and raises
httpx.HTTPStatusError as intended.
@claude

claude Bot commented Feb 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@Kamilbenkirane Kamilbenkirane merged commit a92286b into main Feb 23, 2026
11 checks passed
@Kamilbenkirane Kamilbenkirane mentioned this pull request Feb 24, 2026
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.

_handle_error_response raises UnicodeDecodeError on binary error bodies

1 participant