Commit 7c297bf
fix: Correctly catch JSONDecodeError exceptions (#26)
Problem:
Since `requests` version `2.27.0` [1], the library has introduced its
own `JSONDecodeError` exception, to fix inconsistencies between `json`
and `simplejson`.
From that version onwards, if the project has `simplejson` installed,
the exception raised by `json()` is a subclass of
`simplejson.JSONDecodeError` instead of `json.JSONDecodeError`.
The Knock client is only handling `json.JSONDecodeError`, so in projects
where `simplejson` is installed, the exception is raised when `json()`
is not able to decode the response body (e.g. in 204 responses).
How to reproduce:
In a new virtualenv, install `requests`.
```python
>>> import json, requests
>>> isinstance(requests.exceptions.JSONDecodeError("", "", 0), json.JSONDecodeError)
True
```
Now, install `simplejson`.
```python
>>> import json, requests, simplejson
>>> isinstance(requests.exceptions.JSONDecodeError("", "", 0), json.JSONDecodeError)
False
>>> isinstance(requests.exceptions.JSONDecodeError("", "", 0), simplejson.JSONDecodeError)
True
```
Solution:
When available, import the `JSONDecodeError` class provided by
`requests`, and use that one to handle exceptions from the `json()`
method. Otherwise, first try to use the exception provided by
`simplejson`, and only use the one from `json` if none is available.
[1] psf/requests@db575ee
Co-authored-by: Chris Bell <cjbell1988@gmail.com>1 parent d863b36 commit 7c297bf
1 file changed
+9
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
5 | 13 | | |
6 | 14 | | |
7 | 15 | | |
| |||
0 commit comments