Check the HTTP status code for JSON responses
Maintainers usually reply within 3 days
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start with Sources/OpenAI/Private/JSONResponseDecoder.swift and URLResponseHandler, then compare the existing status handling in StreamingSession and throwIfErrorStatus. Check relaxed parsing behavior for supported providers and add non-2xx tests for completion-handler, async, and Combine requests, including API and non-API error bodies. Done when all four response paths handle transport errors and status codes consistently and the CHANGELOG describes the changed errors.
Written by the indexing model from the issue text.
Description
JSON requests never check the HTTP status code. They seem to handle API errors only because JSONResponseDecoder falls back to decoding an API error when the expected result type fails to decode (Sources/OpenAI/Private/JSONResponseDecoder.swift). That fallback misses two cases:
- The error body isn't an API error, such as an HTML page from a proxy or gateway. The caller gets a
DecodingErrorabout the expected type instead of an error carrying the status code. - The error body decodes as the expected type. With
.relaxedparsing, which fills in missing required fields, an error body could decode into a result with default values and be returned as a success.
#401 adds a status check for responses read as raw data (files.content(id:) and audioCreateSpeech), in URLResponseHandler.throwIfErrorStatus. It throws the decoded APIErrorResponse for a status of 400 or above, falling back to OpenAIError.statusError, as StreamingSession already does. JSON responses were left out because changing them affects every endpoint.
Proposal
- Route all four
URLResponseHandlermethods (JSON and raw, with and without anerror:parameter) through one function, probablyunwrapwith theresponseadded, that throws a transport error first and then checks the status. That way completion-handler, async and Combine requests behave the same. - Check that providers supported through
.relaxedparsing still work, in particular whether any of them return successful responses with a status of 400 or above, or error bodies in a shapeJSONResponseErrorDecoderdoesn't know. - Add tests with a non-2xx status and an error body, and with a non-API body, for all three flavours.
This changes what JSON requests throw in some error cases (OpenAIError.statusError instead of a DecodingError), so it needs a CHANGELOG entry.
- Dominant language
- Swift
- Stars
- 2.9k
- Forks
- 519
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 13
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MacPaw/OpenAI
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 3 days
-
Enable batch promptsOpen
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MacPaw/OpenAI#73 · 1 comment · 1 reaction ·
Maintainers usually reply within 3 days
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Maintainers usually reply within 3 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
Maintainers usually reply within 3 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
Maintainers usually reply within 3 days
Similar issues
-
builtin-port enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
cmlarsen/writemorebetter#235 ·
Maintainers usually reply within 1 day
-
bug-pending-triage
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
NativeScript/NativeScript#11525 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
callstack/agent-device#3627 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
maziyarpanahi/openmed#4207 · 1 comment ·
Maintainers usually reply within 3 days