Repository navigation
feat(errors): expose response headers on APIError - #188
Conversation
APIError gains headers, set from every HTTP error response and from a refused realtime speech-to-text handshake, so a caller can read x-fish-trace-id and x-fish-error-code.
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. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
The README support-header example is invalid because the separate code block begins with an orphaned except and omits the APIError import, causing syntax and name errors for users who copy it.
1 open issue
What changed in this PR
This PR exposes HTTP response headers through APIError, allowing callers to retrieve diagnostic values such as trace IDs and error codes from failed HTTP requests and refused ASR realtime handshakes.
Changes:
- Added an
httpx.Headersattribute toAPIError, including empty headers when no response exists. - Propagated response headers through synchronous and asynchronous HTTP errors and refused ASR handshakes.
- Added tests, test-server headers, and README guidance while preserving the existing TTS WebSocket upgrade exception behavior.
| File | Description |
|---|---|
| tests/unit/test_error_headers.py | Adds coverage for response-header propagation across HTTP errors, missing headers, and TTS upgrade errors. |
| tests/unit/test_asr_realtime.py | Verifies headers on synchronous and asynchronous refused ASR realtime handshakes. |
| tests/unit/realtime_server.py | Adds diagnostic headers to refused handshake responses in the test server. |
| src/fishaudio/resources/asr_realtime.py | Preserves response headers when converting refused realtime handshake failures into API errors. |
| src/fishaudio/exceptions.py | Adds the optional headers attribute and constructor support to APIError. |
| src/fishaudio/core/client_wrapper.py | Passes HTTP response headers into mapped API exceptions. |
| README.md | Documents accessing diagnostic headers from API and WebSocket errors. |
Review effort: Lite
…ror; cover the non-streaming error paths Also corrects the message docstring, a fixture comment and one README sentence so that each says only what the code does.
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.
…eaders # Conflicts: # README.md

What
When the API refuses a call, the caller can now read the response headers from the error, including
x-fish-trace-idandx-fish-error-code, and pass them to support. Part of FISH-2233.APIErrorgainsheaders. It is set on theAPIErrorraised for an HTTP call and for a refused realtime speech-to-text handshake, sync and async. The SDK names no header and promises none: the error carries whatever the response had.Public interface
APIError.__init__(status, message, body=None, headers=None): a new optional fourth argument, a mapping of header names to values. Inherited byAuthenticationError,PermissionError,NotFoundError,RateLimitErrorandServerError.headerson those six classes: anhttpx.Headers, a copy of what was given, empty when nothing was.Behaviour changes
getattr(err, "headers", None)on anAPIErrorNonehttpx.Headers, possibly emptyjson.dumps(vars(err))on anAPIErrorTypeError:Headersis not JSON serializableExisting two- and three-argument constructor calls, the exception classes raised, and
status,message,bodyandstr(err)are unchanged.Not changed
tts.stream_websocket: a handshake that the server refuses still raiseshttpx_ws.WebSocketUpgradeError, whoseresponse.headersalready has the headers. Wrapping it would break existingexcept WebSocketUpgradeErrorcode. The README shows this path in one line.Calls that fail after they started (an error after the WebSocket opened, a stream that breaks midway) have no error response, so they need a different design.
mainnow has #196, which raises anAPIErrorfor every error response body. This branch is merged with it: #196's body reading is kept as it is, and its_api_error(...)call also passesresponse.headers, so an error whose body is not a JSON object carries the headers too.Verification
poe checkand the test suite pass. Tests intests/unit/test_error_headers.pyrun the client against a local HTTP server and the scripted WebSocket server: an HTTP error carries the headers (sync and async, streaming and non-streaming calls), every error class carries them, a header the response does not have is absent, a header value outside ASCII arrives as sent, an error built by hand has empty headers or takes a value outside ASCII, a refusedtts.stream_websockethandshake exposes them on the upgrade error, and an error whose body isnull,[]or not JSON carries them. The two existing refused-upgrade tests of realtime speech to text assert the headers too. Against production with an invalid key:/v1/tts401 and refused speech-to-text and text-to-speech handshakes expose the trace id andinvalid_api_key; a/wallet401 has neither header.Summary by CodeRabbit