response.json() raises inconsistent exception type
See original GitHub issueThere are some comments about this on https://github.com/psf/requests/issues/4842 but I think it warrants its own issue.
If simplejson
is present in the environment, the library uses it so .json()
returns simplejson.errors.JSONDecodeError
rather than json.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. https://github.com/conda-forge/conda-smithy/pull/1369, https://github.com/Vonage/vonage-python-sdk/issues/197) has to check themselves if simplejson
is present to know what exception type will be thrown.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Python request JSON intermittent Type Error - Stack Overflow
JSONDecodeError: print('response was not a JSON string, but', ... It does not necessarily mean that r.json() is not a valid JSON.
Read more >18 SQL/JSON Function JSON_VALUE - Oracle Help Center
If json_value targets a JSON object, and you specify a user-defined SQL object type as the return type, then json_value returns an instance...
Read more >An Exploration of JSON Interoperability Vulnerabilities
JSON INTEROPERABILITY SECURITY RISKS · 1. Inconsistent Duplicate Key Precedence · 2. Key Collision: Character Truncation and Comments · 3. JSON ...
Read more >Troubleshooting Amazon CloudSearch - AWS Documentation
First, make sure that the batch is valid XML or JSON. If it is, check for invalid document IDs and make sure you...
Read more >How to use the requests.exceptions.HTTPError function ... - Snyk
HTTPError as exc: logger.exception(exc) if response.json()['error_code'] ... request_object.raise_for_status() except HTTPError as e: # raise detailed error ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Maybe the simplest solution which wouldn’t break existing code is to provide a
requests.JSONDecodeError
which aliases whatever exception type will be thrown, and calling code should always use that.@sigmavirus24 I can make that edit in my second branch. I didn’t do any
try
/except
for importing theJSONDecodeError
fromsimplejson
orjson
, so I’ll do that and then I’ll submit a PR.