Skip to content

Commit

Permalink
Initialize JsonDecodeError before initializing IOError
Browse files Browse the repository at this point in the history
That way we get the formated error message
  • Loading branch information
cHYzZQo committed Jan 8, 2022
1 parent 620ed4f commit 1a0b870
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 7 additions & 0 deletions requests/exceptions.py
Expand Up @@ -34,6 +34,13 @@ class InvalidJSONError(RequestException):
class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError):
"""Couldn't decode the text into json"""

def __init__(self, *args, **kwargs):
# Construct the JSONDecodeError instance first with all
# args. Then use it's args to construct the IOError so that
# its json specific args aren't used as IOError specific args
CompatJSONDecodeError.__init__(self, *args)
InvalidJSONError.__init__(self, *self.args, **kwargs)


class HTTPError(RequestException):
"""An HTTP error occurred."""
Expand Down
9 changes: 6 additions & 3 deletions tests/test_requests.py
Expand Up @@ -23,14 +23,14 @@
cookiejar_from_dict, morsel_to_cookie)
from requests.exceptions import (
ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL,
MissingSchema, ReadTimeout, Timeout, RetryError, TooManyRedirects,
MissingSchema, ReadTimeout, Timeout, RetryError, RequestException, TooManyRedirects,
ProxyError, InvalidHeader, UnrewindableBodyError, SSLError, InvalidProxyURL, InvalidJSONError)
from requests.models import PreparedRequest
from requests.structures import CaseInsensitiveDict
from requests.sessions import SessionRedirectMixin
from requests.models import urlencode
from requests.hooks import default_hooks
from requests.compat import MutableMapping
from requests.compat import JSONDecodeError, MutableMapping

from .compat import StringIO, u
from .utils import override_environ
Expand Down Expand Up @@ -2585,5 +2585,8 @@ def test_post_json_nan(self, httpbin):

def test_json_decode_compatibility(self, httpbin):
r = requests.get(httpbin('bytes/20'))
with pytest.raises(requests.exceptions.JSONDecodeError):
with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:
r.json()
assert isinstance(excinfo.value, RequestException)
assert isinstance(excinfo.value, JSONDecodeError)
assert "bytes/20" not in str(excinfo.value)

0 comments on commit 1a0b870

Please sign in to comment.