Skip to content

Commit

Permalink
Import ConnectionClosed from websockets.exceptions.
Browse files Browse the repository at this point in the history
Fix #1539.
  • Loading branch information
aaugustin committed Nov 11, 2024
1 parent e9fc77d commit 083bcac
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/faq/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ How do I reconnect when the connection drops?
Use :func:`~websockets.asyncio.client.connect` as an asynchronous iterator::

from websockets.asyncio.client import connect
from websockets.exceptions import ConnectionClosed

async for websocket in connect(...):
try:
...
except websockets.ConnectionClosed:
except ConnectionClosed:
continue

Make sure you handle exceptions in the ``async for`` loop. Uncaught exceptions
Expand Down
2 changes: 1 addition & 1 deletion docs/faq/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Then, call :meth:`~ServerConnection.send`::

async def message_user(user_id, message):
websocket = CONNECTIONS[user_id] # raises KeyError if user disconnected
await websocket.send(message) # may raise websockets.ConnectionClosed
await websocket.send(message) # may raise websockets.exceptions.ConnectionClosed

Add error handling according to the behavior you want if the user disconnected
before the message could be sent.
Expand Down
4 changes: 3 additions & 1 deletion docs/intro/tutorial1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,13 @@ spot real errors when you add functionality to the server. Catch it in the

.. code-block:: python
from websockets.exceptions import ConnectionClosedOK
async def handler(websocket):
while True:
try:
message = await websocket.recv()
except websockets.ConnectionClosedOK:
except ConnectionClosedOK:
break
print(message)
Expand Down
2 changes: 1 addition & 1 deletion src/websockets/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class connect:
async for websocket in connect(...):
try:
...
except websockets.ConnectionClosed:
except websockets.exceptions.ConnectionClosed:
continue
If the connection fails with a transient error, it is retried with
Expand Down
2 changes: 1 addition & 1 deletion src/websockets/legacy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class Connect:
async for websocket in connect(...):
try:
...
except websockets.ConnectionClosed:
except websockets.exceptions.ConnectionClosed:
continue
The connection is closed automatically after each iteration of the loop.
Expand Down

0 comments on commit 083bcac

Please sign in to comment.