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

Replace RuntimeError with WebSocketDisconnected #2767

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions starlette/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def __init__(self, code: int = 1000, reason: str | None = None) -> None:
self.reason = reason or ""


class WebSocketDisconnected(RuntimeError):
"""
Raised when attempting to use a disconnected WebSocket.
"""

pass
Comment on lines +25 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not really what I meant... The exception already exists.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, reading the code, WebSocketDisconnect seems to be called when you want to disconnect a socket, in these cases the socket is already disconnected. In my opinion, it would make more sense to create a new exception (WebSocketDisconnected). Tell me if you still want to use WebSocketDisconnect and if so I will make such changes.



class WebSocket(HTTPConnection):
def __init__(self, scope: Scope, receive: Receive, send: Send) -> None:
super().__init__(scope)
Expand Down Expand Up @@ -53,7 +61,7 @@ async def receive(self) -> Message:
self.client_state = WebSocketState.DISCONNECTED
return message
else:
raise RuntimeError('Cannot call "receive" once a disconnect message has been received.')
raise WebSocketDisconnected('Cannot call "receive" once a disconnect message has been received.')

async def send(self, message: Message) -> None:
"""
Expand Down Expand Up @@ -94,7 +102,7 @@ async def send(self, message: Message) -> None:
self.application_state = WebSocketState.DISCONNECTED
await self._send(message)
else:
raise RuntimeError('Cannot call "send" once a close message has been sent.')
raise WebSocketDisconnected('Cannot call "send" once a close message has been sent.')

async def accept(
self,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from starlette.responses import Response
from starlette.testclient import WebSocketDenialResponse
from starlette.types import Message, Receive, Scope, Send
from starlette.websockets import WebSocket, WebSocketDisconnect, WebSocketState
from starlette.websockets import WebSocket, WebSocketDisconnect, WebSocketDisconnected, WebSocketState
from tests.types import TestClientFactory


Expand Down Expand Up @@ -448,7 +448,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
await websocket.close()

client = test_client_factory(app)
with pytest.raises(RuntimeError):
with pytest.raises(WebSocketDisconnected):
with client.websocket_connect("/"):
pass # pragma: no cover

Expand All @@ -462,7 +462,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
message = await websocket.receive()

client = test_client_factory(app)
with pytest.raises(RuntimeError):
with pytest.raises(WebSocketDisconnected):
with client.websocket_connect("/") as websocket:
websocket.close()

Expand Down