Skip to content

fix: raise BadResponse when a response is not valid JSON - #31

Draft
chipkent wants to merge 1 commit into
fix/bad-response-statusfrom
fix/bad-json-response
Draft

chipkent wants to merge 1 commit into
fix/bad-response-statusfrom
fix/bad-json-response

Conversation

@chipkent

@chipkent chipkent commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Preview only. This is a draft in the fork showing the exact text of the PR planned for massive-com/client-python. It will be closed, not merged, once the upstream PR is opened.

Fixes #<issue: F5c>

Builds on #12 (BadResponse.status).

When a 200 response body is not valid JSON (for example an HTML page from a proxy, or a truncated body), BaseClient._get and _paginate_iter log the decode error and return []. A get_* call returns [] instead of a model, and a list_* call yields nothing if the first page is bad, or stops early if a later page is bad. Either way the caller gets missing data with no error.

  • _decode now raises BadResponse with status=resp.status and the first 200 bytes of the body; the json error is chained as __cause__.
  • _get and _paginate_iter drop their try/except and let it propagate. These are the only two places that decode a response.
  • The BadResponse docstring now covers this case.

Valid responses are unaffected. A call that used to return [] for a body it could not decode now raises BadResponse, the exception callers already handle for non-200 responses.

try:
    trades = list(client.list_trades("AAPL"))
except BadResponse as e:
    print(e.status, e)  # 200 Invalid JSON in response: <html>...

Tests in test_rest/test_bad_response.py use pook to serve a non-JSON body to a get_* call, to the first page of a list_* call, and to its second page. All three fail without the fix.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

When a 200 response body was not valid JSON, BaseClient._get logged the
decode error and returned [], and _paginate_iter did the same. A get_*
call returned [] instead of a model, and a list_* call yielded nothing
if the first page was bad, or stopped early, without error, if a later
page was bad. Callers silently got missing data.

_decode now raises BadResponse with the response status and the first
200 bytes of the body, and _get and _paginate_iter let it propagate.
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.

1 participant