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

Preserve exception context in Client.connect #1450

Merged
merged 2 commits into from
Apr 11, 2025

Conversation

tvanbaak
Copy link
Contributor

@tvanbaak tvanbaak commented Apr 8, 2025

Currently, connection errors give a stack trace like this:

Traceback (most recent call last):
  File "...", line 37, in <module>
    main()
  File "...", line 19, in main
    client.connect(args.url, namespace=args.namespace, transports=["websocket"])
  File ".../socketio/simple_client.py", line 82, in connect
    self.client.connect(url, headers=headers, auth=auth,
  File ".../socketio/client.py", line 159, in connect
    raise exceptions.ConnectionError(exc.args[0]) from None
socketio.exceptions.ConnectionError: Connection error

This discards the stack trace of exc, which can be useful for figuring out what the connection error was. Raising from exc preserves this:

Traceback (most recent call last):
  File ".../engineio/client.py", line 332, in _connect_websocket
    ws = websocket.create_connection(
  File ".../websocket/_core.py", line 646, in create_connection
    websock.connect(url, **options)
  File ".../websocket/_core.py", line 256, in connect
    self.sock, addrs = connect(
  File ".../websocket/_http.py", line 145, in connect
    sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
  File ".../websocket/_http.py", line 222, in _open_socket
    raise error
  File ".../websocket/_http.py", line 209, in _open_socket
    sock.connect(address)
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../socketio/client.py", line 147, in connect
    self.eio.connect(real_url, headers=real_headers,
  File ".../engineio/client.py", line 95, in connect
    return getattr(self, '_connect_' + self.transports[0])(
  File ".../engineio/client.py", line 340, in _connect_websocket
    raise exceptions.ConnectionError('Connection error')
engineio.exceptions.ConnectionError: Connection error

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

Traceback (most recent call last):
  File "...", line 37, in <module>
    main()
  File "...", line 19, in main
    client.connect(args.url, namespace=args.namespace, transports=["websocket"])
  File ".../socketio/simple_client.py", line 82, in connect
    self.client.connect(url, headers=headers, auth=auth,
  File ".../socketio/client.py", line 159, in connect
    raise exceptions.ConnectionError(exc.args[0]) from exc
socketio.exceptions.ConnectionError: Connection error

@miguelgrinberg
Copy link
Owner

@tvanbaak Could you please make the same change in the async client so that both are consistent? Thanks!

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (6a52e8b) to head (d74c3ef).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1450   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           31        31           
  Lines         2530      2530           
  Branches       432       432           
=========================================
  Hits          2530      2530           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@miguelgrinberg miguelgrinberg merged commit 5c93c59 into miguelgrinberg:main Apr 11, 2025
19 of 22 checks passed
@miguelgrinberg
Copy link
Owner

Thanks!

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.

3 participants