Skip to content

Conversation

@lovelydinosaur
Copy link
Contributor

Closes #1172.

Instead of...

$ venv/bin/python ./example.py 
Traceback (most recent call last):
  File "./example.py", line 2, in <module>
    r = httpx.get('https://www.example.org/')
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_api.py", line 170, in get
    trust_env=trust_env,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_api.py", line 96, in request
    allow_redirects=allow_redirects,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 664, in request
    request, auth=auth, allow_redirects=allow_redirects, timeout=timeout,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 694, in send
    request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 723, in _send_handling_redirects
    request, auth=auth, timeout=timeout, history=history
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 759, in _send_handling_auth
    response = self._send_single_request(request, timeout)
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 793, in _send_single_request
    timeout=timeout.as_dict(),
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_exceptions.py", line 359, in map_exceptions
    raise mapped_exc(message, **kwargs) from None  # type: ignore
httpx.ReadError: Server disconnected

We'll instead see...

Traceback (most recent call last):
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_exceptions.py", line 342, in map_exceptions
    yield
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 793, in _send_single_request
    timeout=timeout.as_dict(),
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection_pool.py", line 189, in request
    method, url, headers=headers, stream=stream, timeout=timeout
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection.py", line 96, in request
    return self.connection.request(method, url, headers, stream, timeout)
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/http11.py", line 73, in request
    ) = self._receive_response(timeout)
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/http11.py", line 130, in _receive_response
    event = self._receive_event(timeout)
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/http11.py", line 160, in _receive_event
    data = self.socket.read(self.READ_NUM_BYTES, timeout)
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_backends/sync.py", line 56, in read
    raise ReadError("Server disconnected")
httpcore.ReadError: Server disconnected

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./example.py", line 2, in <module>
    r = httpx.get('https://www.example.org/')
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_api.py", line 170, in get
    trust_env=trust_env,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_api.py", line 96, in request
    allow_redirects=allow_redirects,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 664, in request
    request, auth=auth, allow_redirects=allow_redirects, timeout=timeout,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 694, in send
    request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 723, in _send_handling_redirects
    request, auth=auth, timeout=timeout, history=history
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 759, in _send_handling_auth
    response = self._send_single_request(request, timeout)
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 793, in _send_single_request
    timeout=timeout.as_dict(),
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/tomchristie/GitHub/encode/httpx/httpx/_exceptions.py", line 359, in map_exceptions
    raise mapped_exc(message, **kwargs) from exc  # type: ignore
httpx.ReadError: Server disconnected

@lovelydinosaur lovelydinosaur added the user-experience Ensuring that users have a good experience using the library label Aug 19, 2020
@j178
Copy link
Contributor

j178 commented Aug 19, 2020

Should we port this to httpcore's map_exceptions as well?

@lovelydinosaur
Copy link
Contributor Author

I think it probably depends. Do we want tracebacks into h11 and h2? Yup, that'd make sense.

Do we want tracebacks into Python's stdlib? Probably not.

@lovelydinosaur lovelydinosaur merged commit 25507ac into master Aug 19, 2020
@lovelydinosaur lovelydinosaur deleted the include-underlying-traceback branch August 19, 2020 16:38
@lovelydinosaur lovelydinosaur mentioned this pull request Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

user-experience Ensuring that users have a good experience using the library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How about attaching original traceback to exceptions raised in map_exceptions() ?

2 participants