Repository navigation
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix ClientError arguments and crashes on unusable 4xx bodies #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Fix ClientError arguments and crashes on unusable 4xx bodies #318
Changes from all commits
0bc4e2dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
`API._handle_exception` has two problems, both in the 4xx branch. The two fallback `raise`s pass their arguments in the wrong positions. `ClientError.__init__` is `(status_code, error_code, error_message, header, error_data)`, but they pass `(status_code, None, response.text, None, response.headers)`, so `header` ends up `None` and the HTTP headers land in `error_data`. The success path gets this right, so the same exception carries headers in different attributes depending on the response body. This is the ordinary path, not an edge case: the API answers a bad request with a plain-text body ("Failed to deserialize the JSON body into the target type"), which is not valid JSON, so every real 4xx takes the `JSONDecodeError` fallback and loses its headers. Anything reading `err.header` to back off on a rate limit sees `None`. Second, the body is only checked for `None` before being used as a mapping. `json.loads` returns `None` only for the literal `null`; a JSON string, array or number is equally not a dict, and `err.get("data")` raises `AttributeError` on all three. An object that lacks `code` or `msg` -- as an intermediary with its own error schema would return -- raises `KeyError`. Either way the caller gets an exception the SDK does not define, in place of the `ClientError` it catches. Accept a body only when it is a dict carrying both keys, and otherwise raise a `ClientError` holding the raw text as the message, with the headers in `header`. Adds tests/api_test.py: the well-formed body is unpacked as before, 5xx still raises `ServerError`, and eight unusable bodies -- including the plain-text one the live API returns -- all produce a well-formed `ClientError`. Eight of the eleven fail before this change.Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing