Skip to content
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

fix: Correctly catch JSONDecodeError exceptions #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

adamantike
Copy link

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.

>>> import json, requests
>>> isinstance(requests.exceptions.JSONDecodeError("", "", 0), json.JSONDecodeError)
True

Now, install simplejson.

>>> 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.

[1] psf/requests@db575ee

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
@adamantike adamantike force-pushed the fix/correctly-catch-json-decode-errors branch from 5b910b7 to 94beb00 Compare February 27, 2024 15:04
@adamantike
Copy link
Author

@cjbell, gentle ping, based on previous commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant