Skip to content
Myr

Initialize JsonDecodeError before initializing IOError (#6036)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

That way we get the formated error message

authored by chyzzqo2 · committed by GitHub · parent d15a3b6 · Browse files · 2 files+23 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Peter <cHYzZQo@netflix.com>

Diff stats

2 files+23 added−3 removed

  • tests:+13 added−3 removed
  • other:+10 added−0 removed

2 files+23 added−3 removed

modified:requests/exceptions.py+10 added
32 unmodified lines
3334class JSONDecodeError(InvalidJSONError, CompatJSONDecodeError):35 """Couldn't decode the text into json"""3637 def __init__(self, *args, **kwargs):38 """39 Construct the JSONDecodeError instance first with all40 args. Then use it's args to construct the IOError so that41 the json specific args aren't used as IOError specific args42 and the error message from JSONDecodeError is preserved.43 """44 CompatJSONDecodeError.__init__(self, *args)45 InvalidJSONError.__init__(self, *self.args, **kwargs)464748class HTTPError(RequestException):49 """An HTTP error occurred."""5051
92 unmodified lines
modified:tests/test_requests.py+13 added-3 removed
21 unmodified lines
22from requests.cookies import (23 cookiejar_from_dict, morsel_to_cookie)24from requests.exceptions import (25 ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL,26 MissingSchema, ReadTimeout, Timeout, RetryError, TooManyRedirects,26 MissingSchema, ReadTimeout, Timeout, RetryError, RequestException, TooManyRedirects,27 ProxyError, InvalidHeader, UnrewindableBodyError, SSLError, InvalidProxyURL, InvalidJSONError)28from requests.models import PreparedRequest29from requests.structures import CaseInsensitiveDict30from requests.sessions import SessionRedirectMixin31from requests.models import urlencode32from requests.hooks import default_hooks33from requests.compat import MutableMapping33from requests.compat import JSONDecodeError, is_py3, MutableMapping3435from .compat import StringIO, u36from .utils import override_environ37from urllib3.util import Timeout as Urllib3Timeout
2546 unmodified lines
2584 r = requests.post(httpbin('post'), json=data)25852586 def test_json_decode_compatibility(self, httpbin):2587 r = requests.get(httpbin('bytes/20'))2588 with pytest.raises(requests.exceptions.JSONDecodeError):2588 with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:2589 r.json()2590 assert isinstance(excinfo.value, RequestException)2591 assert isinstance(excinfo.value, JSONDecodeError)2592 assert r.text not in str(excinfo.value)25932594 @pytest.mark.skipif(not is_py3, reason="doc attribute is only present on py3")2595 def test_json_decode_persists_doc_attr(self, httpbin):2596 r = requests.get(httpbin('bytes/20'))2597 with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:2598 r.json()2599 assert excinfo.value.doc == r.text