-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
response.json() raises inconsistent exception type #5794
Comments
Maybe the simplest solution which wouldn't break existing code is to provide a |
Any update :)? |
I don't see a easy way to do this without breaking existing code for other developers because in the requests/models.py (around line 881). It looks like the code intentionally raises different exceptions.
|
The more I think about it... |
I actually think the long term solution would be to stop using simplejson but maybe until then give callers a way to control which exception they're going to get, e.g. |
It is probably easier to add a flag (or argument) to the json method that catches all the errors and raises only 1 error like requests.JSONDecodeError. If simplejson is intended to be phased out then adding logic such that if std_json is True then raise only json.JSONDecodeError if any error has occured. If std_json is False then old behaviour happens??? |
Hi. I've been working on ways to fix this issue, and so far, I've found two ways. The most ideal way seems to be replacing usage of
I agree with this as a second option, although, in the long run, it may eventually be good to use option 1. If we did this in class JSONDecodeError(json.JSONDecodeError, simplejson.JSONDecodeError):
"""Couldn't decode the text into json""" ...it could get thrown if |
@rowanseymour @BarryThrill @wwyl1234 I'm updating you on this since I made branches in a fork for the two best fixes for this issue. Fix option 1 (not ideal for now) - replace Fix option 2 (more ideal for now) - replace @nateprewitt @sigmavirus24 @sethmlarson I was told to contact you for maintenance requests. Could I have permission to submit a pull request for one of these branches? P.S. I ran tests and they passed. Thanks |
There is more than 2 options and there is only one truly ideal option - one that preserves backwards compatibility. The standard library raises try:
from json import JSONDecodeError as stdlib_JSONDecodeError
except ImportError:
stdlib_JSONDecodeError = ValueError
try:
from simplejson import JSONDecodeError as sj_JSONDecodeError
except ImportError:
sj_JSONDecodeError = Exception
class JSONDecodeError(stdlib_JSONDecodeError, sj_JSONDecodeError):
pass This avoids the unnecessary removal of simplejson support for those that still expect it. This also allows folks catching one of:
To still catch this exception too without having to change code |
@sigmavirus24 I can make that edit in my second branch. I didn't do any |
`requests` is somewhat inconsistent with how it raises this exception. (psf/requests#5794) Depending on the environment, the following can happen during `response.json`: - Python2 and no `simplejson`, a `ValueError` will be raised - Python2 and `simplejson`, a `simplejson.JSONDecodeError` will be raised - Python3 and no `simplejson`, a `json.JSONDecodeError` will be raised - Python3 and `simplejson`, a `simplejson.JSONDecodeError` will be raised The following wil make sure that catching `transifex.api.jsonapi.compat.JSONDecodeError` in a `try: response.json()` block will always work
There are some comments about this on #4842 but I think it warrants its own issue.
If
simplejson
is present in the environment, the library uses it so.json()
returnssimplejson.errors.JSONDecodeError
rather thanjson.decoder.JSONDecodeError
If I'm writing a library that uses
requests
I don't know what other libraries will be present in an environment. I expect that a method returns a consistent exception type that I can handle. As it stands, anyone writing a library (e.g. conda-forge/conda-smithy#1369, Vonage/vonage-python-sdk#197) has to check themselves ifsimplejson
is present to know what exception type will be thrown.The text was updated successfully, but these errors were encountered: