#Inconsistent exception for disconnected ws

1 messages · Page 1 of 1 (latest)

flint vault
#

In WebSocket.wrapped_receive() we raise WebSocketException if a connection is already disconnected:

https://github.com/litestar-org/litestar/blob/bd596874209aebc928e5dc5192eec0b7f7033667/litestar/connection/websocket.py#L73-L75

However, in receive_data we raise the WebSocketDisconnect exception for the same condition:

https://github.com/litestar-org/litestar/blob/bd596874209aebc928e5dc5192eec0b7f7033667/litestar/connection/websocket.py#L156-L171

I'm pretty green on this ws stuff, anyone know if this is by design, or likely an oversight?

delicate canopyBOT
#

litestar/connection/websocket.py lines 73 to 75

async def wrapped_receive() -> ReceiveMessage:
    if self.connection_state == "disconnect":
        raise WebSocketException(detail=DISCONNECT_MESSAGE)

litestar/connection/websocket.py lines 156 to 171

async def receive_data(self, mode: WebSocketMode) -> str | bytes:
    """Receive an 'websocket.receive' event and returns the data stored on it.

    Args:
        mode: The respective event key to use.

    Returns:
        The event's data.
    """
    if self.connection_state == "init":
        await self.accept()
    event = cast("WebSocketReceiveEvent | WebSocketDisconnectEvent", await self.receive())
    if event["type"] == "websocket.disconnect":
        raise WebSocketDisconnect(detail="disconnect event", code=event["code"])
    if self.connection_state == "disconnect":
        raise WebSocketDisconnect(detail=DISCONNECT_MESSAGE)  # pragma: no cover