Skip to content
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

Chain exception re-raise in map_exceptions #678

Merged
merged 2 commits into from
May 10, 2023
Merged

Conversation

zanieb
Copy link
Contributor

@zanieb zanieb commented May 5, 2023

Preserves the exception chain instead of displaying "During handling of the above exception, another exception occurred" in the traceback.

Related to #677

Preserves the exception chain instead of displaying "During handling of the above exception, another exception occurred" in the traceback
@zanieb
Copy link
Contributor Author

zanieb commented May 5, 2023

I'd like to get a few concrete before / after examples for this still.

@tomchristie
Copy link
Member

tomchristie commented May 10, 2023

Makes sense, yup.

Thanks 😌

I'd like to get a few concrete before / after examples for this still

I verified this with the following...

before:

>>> import httpcore
>>> httpcore.request("GET", "https://blahblahblahnope/")
Traceback (most recent call last):
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/backends/sync.py", line 94, in connect_tcp
    sock = socket.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 826, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 961, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_api.py", line 39, in request
    return pool.request(
           ^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/interfaces.py", line 43, in request
    response = self.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection_pool.py", line 253, in handle_request
    raise exc
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection_pool.py", line 237, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection.py", line 86, in handle_request
    raise exc
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection.py", line 63, in handle_request
    stream = self._connect(request)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection.py", line 111, in _connect
    stream = self._network_backend.connect_tcp(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/backends/sync.py", line 93, in connect_tcp
    with map_exceptions(exc_map):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc)
httpcore.ConnectError: [Errno 8] nodename nor servname provided, or not known

after:

>>> import httpcore
>>> httpcore.request("GET", "https://blahblahblahnope/")
Traceback (most recent call last):
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/backends/sync.py", line 94, in connect_tcp
    sock = socket.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 826, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 961, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

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

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_api.py", line 39, in request
    return pool.request(
           ^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/interfaces.py", line 43, in request
    response = self.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection_pool.py", line 253, in handle_request
    raise exc
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection_pool.py", line 237, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection.py", line 86, in handle_request
    raise exc
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection.py", line 63, in handle_request
    stream = self._connect(request)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_sync/connection.py", line 111, in _connect
    stream = self._network_backend.connect_tcp(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/backends/sync.py", line 93, in connect_tcp
    with map_exceptions(exc_map):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/tomchristie/GitHub/encode/httpcore/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ConnectError: [Errno 8] nodename nor servname provided, or not known

And yes, the change of traceback from "During handling of the above exception, another exception occurred" to "The above exception was the direct cause of the following exception" makes complete sense.

I'll leave the merge to you.

@zanieb zanieb merged commit 78756bf into master May 10, 2023
@zanieb zanieb deleted the chain-map-exceptions branch May 10, 2023 15:36
@Pliner Pliner mentioned this pull request May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants