Skip to content

fix: raise APIError for every error response body - #196

Merged
lumpinif merged 3 commits into
mainfrom
fix/fish-2270-small-fixes
Nov 30, 2026
Merged

lumpinif merged 3 commits into
mainfrom
fix/fish-2270-small-fixes

Conversation

@lumpinif

@lumpinif lumpinif commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Some error responses made the SDK raise something other than APIError, so except APIError did not catch them. They come from bodies a gateway or proxy can send. Each review found one more exception type, so the body is now read behind a single boundary instead of catching types one by one: decoding and parsing an error body cannot raise. _raise_for_status raises the APIError subclass for the status, whatever the body is.

Error body Before After
JSON object with message / detail APIError, that message (message first) unchanged
not JSON APIError, the body text unchanged
empty APIError, status phrase unchanged
JSON null, number, boolean, string, list AttributeError APIError; message = body text
bytes that are not valid in the charset (e.g. 0xff as UTF-8) UnicodeDecodeError APIError; text with replacement characters
charset=utf-16 / utf-32 without a byte order mark UnicodeError APIError; body decoded as UTF-8 with replacement
a charset naming a non-text codec (rot_13, base64_codec, …) TypeError / AssertionError APIError; body decoded as UTF-8 with replacement
JSON nested deeper than the parser allows RecursionError APIError; message = body text

How the message is chosen is unchanged; body stays the decoded text. Tests: tests/unit/test_error_body.py, 87 tests over sync, sync streaming, async and async streaming, including a seeded test of 300 random bodies and charsets (every stdlib codec name) per path that accepts only APIError. Conflicts with #188 on the raise line; whichever merges second passes response.headers as the fourth argument.

_raise_for_status raised something other than APIError for some error bodies
that a gateway or proxy can send:

- JSON that is not an object (null, a number, a boolean, a string, a list):
  AttributeError from .get.
- Bytes that are not valid UTF-8, such as 0xff: UnicodeDecodeError from
  response.json(), which was not caught.
- A body declared as charset=utf-16 without a byte order mark: UnicodeError
  from response.text.

Now every error response raises the APIError subclass for its status. The
message is the message, else detail, field of a JSON object; for any other
body it is the body text, or the status phrase when the body is empty. body
is the decoded text, as before; a body that cannot be decoded with its charset
is decoded as UTF-8 with replacement characters.
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 38 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8d152e1d-89ee-47f5-9365-15d69f6f4774
Commits

Reviewing files that changed from the base of the PR and between 245d16b and 1bdb12e.

Files selected for processing: 2

  • src/fishaudio/core/client_wrapper.py
  • tests/unit/test_error_body.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/fishaudio/core/client_wrapper.py 94.51% <100.00%> (+1.70%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Reviews kept finding one more exception type that escaped _raise_for_status:
RecursionError for JSON nested deeper than the parser's limit, and TypeError
or AssertionError for a charset that names a codec that is not a text
encoding (rot_13, base64_codec, zlib_codec, hex_codec, ...).

Decoding and parsing the body now happen in one function that never raises:
any failure (Exception) means "no parsed JSON" and a best-effort text, UTF-8
with replacement characters as the last resort, or empty text if the body
cannot be read at all. _raise_for_status raises the APIError subclass for the
status whatever the body is. How the message is chosen is unchanged.

Tests: the reviewers' cases (nested JSON, non-text codecs, BOM-less UTF-16 and
UTF-32 with non-ASCII text), an unread body, and a seeded test that sends 300
random bodies with random charsets (every stdlib codec name, an unknown name,
none) through each of the four request paths and accepts only APIError.
The docstrings and the test module said every error response raises APIError
and that reading the body never raises. Errors while the body is being
received (a transport error, a body that fails to decompress) are raised by
httpx before the SDK sees the response, and BaseExceptions such as
cancellation propagate on purpose. Say only that.
@lumpinif
lumpinif marked this pull request as ready for review November 30, 2026 14:52
Copilot AI lite review requested due to automatic review settings November 30, 2026 14:52
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-30T14:52:59.802641Z
We couldn’t complete this review. Comment @codex review to try again.
1bdb12e Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@lumpinif
lumpinif merged commit f8c5c71 into main Nov 30, 2026
28 checks passed
@lumpinif
lumpinif deleted the fix/fish-2270-small-fixes branch November 30, 2026 14:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The error-construction line overlaps open PR #188, so approval should wait until the merged implementation is verified to preserve response headers across HTTP error subclasses and retain the new body fallback behavior.

No open issues 🎉

What changed in this PR

This PR hardens HTTP error handling so malformed, non-object, incorrectly encoded, or otherwise unexpected error response bodies consistently raise the appropriate APIError subclass.

Changes:

  • Added centralized error-body decoding and JSON parsing with safe UTF-8 replacement fallbacks.
  • Updated _raise_for_status to select messages from JSON objects or fall back to decoded body text and status phrases.
  • Added extensive sync, async, streaming, charset, malformed-body, recursion, and unread-body tests.
File Description
tests/​unit/​test_error_body.py Adds comprehensive coverage for malformed, non-object, invalidly encoded, deeply nested, empty, sync, async, and streaming error responses.
src/​fishaudio/​core/​client_wrapper.py Safely decodes and parses error bodies before raising status-specific APIError subclasses.

Review effort: Lite

lumpinif added a commit that referenced this pull request Dec 1, 2026
Keeps main's error-body reading (#196) and passes the response headers
to its APIError. A test checks that an error whose body is not a JSON
object carries the headers.
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.

2 participants