Repository navigation
Commit 0bc4e2d
committed
Fix ClientError arguments and crashes on unusable 4xx bodies
`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.1 parent 2fdb18f commit 0bc4e2d
2 files changed
Lines changed: 77 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
43 | 46 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
0 commit comments