Repository navigation
fix: raise APIError for every error response body - #196
Conversation
_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.
|
Warning Review limit reachedYou'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. View limit details
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. |
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
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.
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
🔵 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_statusto 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
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.
Some error responses made the SDK raise something other than
APIError, soexcept APIErrordid 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_statusraises theAPIErrorsubclass for the status, whatever the body is.message/detailAPIError, that message (message first)APIError, the body textAPIError, status phraseAttributeErrorAPIError; message = body text0xffas UTF-8)UnicodeDecodeErrorAPIError; text with replacement characterscharset=utf-16/utf-32without a byte order markUnicodeErrorAPIError; body decoded as UTF-8 with replacementrot_13,base64_codec, …)TypeError/AssertionErrorAPIError; body decoded as UTF-8 with replacementRecursionErrorAPIError; message = body textHow the message is chosen is unchanged;
bodystays 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 onlyAPIError. Conflicts with #188 on theraiseline; whichever merges second passesresponse.headersas the fourth argument.