WebSocket Exceptions¶
Exception hierarchy for the websocket subsystem.
Exception Hierarchy¶
KstlibError
└── WebSocketError
├── WebSocketConnectionError
├── WebSocketClosedError
├── WebSocketTimeoutError
├── WebSocketReconnectError
├── WebSocketProtocolError
└── WebSocketQueueFullError
Base Exception¶
Connection Errors¶
- exception kstlib.websocket.exceptions.WebSocketConnectionError(message, *, url='', attempts=0, last_error=None)[source]¶
Bases:
WebSocketError,ConnectionErrorFailed to establish WebSocket connection.
Raised when the initial connection or reconnection fails after exhausting all retry attempts.
- url¶
The WebSocket URL that failed to connect.
- attempts¶
Number of connection attempts made.
- last_error¶
The underlying error from the last attempt.
- __init__(self, message: 'str', *, url: 'str' = '', attempts: 'int' = 0, last_error: 'BaseException | None' = None) 'None' -> None[source]¶
Initialize connection error.
- Parameters:
message (str) – Human-readable error description.
url (str) – The WebSocket URL that failed to connect.
attempts (int) – Number of connection attempts made.
last_error (BaseException | None) – The underlying error from the last attempt.
Raised when connection fails:
Initial connection failure
All retry attempts exhausted
DNS resolution failure
Attributes:
url: The WebSocket URL that failedattempts: Number of connection attempts madelast_error: The underlying error from the last attempt
Closed Errors¶
- exception kstlib.websocket.exceptions.WebSocketClosedError(message, *, code=1006, reason='')[source]¶
Bases:
WebSocketErrorWebSocket connection was closed.
Raised when the connection is closed unexpectedly by the server or due to a protocol error.
- code¶
WebSocket close code (1000-4999).
- reason¶
Optional human-readable close reason.
Raised when connection is closed unexpectedly:
Server initiated close
Protocol error close
Network disconnection
Attributes:
code: WebSocket close code (1000-4999)reason: Human-readable close reason
Timeout Errors¶
- exception kstlib.websocket.exceptions.WebSocketTimeoutError(message, *, operation='', timeout=0.0)[source]¶
Bases:
WebSocketError,TimeoutErrorWebSocket operation timed out.
Raised when a WebSocket operation (connect, ping, receive) exceeds its configured timeout.
- operation¶
The operation that timed out (connect, ping, receive).
- timeout¶
The timeout value in seconds.
Raised when operations time out:
Connection timeout
Ping/pong timeout
Receive timeout
Attributes:
operation: The operation that timed outtimeout: The timeout value in seconds
Reconnect Errors¶
- exception kstlib.websocket.exceptions.WebSocketReconnectError(message, *, attempts=0, last_error=None)[source]¶
Bases:
WebSocketErrorFailed to reconnect after disconnection.
Raised when all reconnection attempts have been exhausted without successfully re-establishing the connection.
- attempts¶
Total number of reconnection attempts made.
- last_error¶
The underlying error from the last attempt.
- __init__(self, message: 'str', *, attempts: 'int' = 0, last_error: 'BaseException | None' = None) 'None' -> None[source]¶
Initialize reconnect error.
- Parameters:
message (str) – Human-readable error description.
attempts (int) – Total number of reconnection attempts made.
last_error (BaseException | None) – The underlying error from the last attempt.
Raised when reconnection fails:
All reconnection attempts exhausted
Reconnection disabled
Attributes:
attempts: Total reconnection attempts madelast_error: The underlying error from the last attempt
Protocol Errors¶
- exception kstlib.websocket.exceptions.WebSocketProtocolError(message, *, protocol_error='')[source]¶
Bases:
WebSocketErrorWebSocket protocol violation.
Raised when the server or client violates the WebSocket protocol, such as sending malformed frames or invalid data.
- protocol_error¶
Description of the protocol violation.
Raised on protocol violations:
Malformed frames
Invalid data format
Protocol state violations
Attributes:
protocol_error: Description of the protocol violation
Queue Errors¶
- exception kstlib.websocket.exceptions.WebSocketQueueFullError(message, *, queue_size=0, dropped_count=0)[source]¶
Bases:
WebSocketErrorMessage queue is full.
Raised when the incoming message queue is full and cannot accept more messages. This typically indicates the consumer is too slow.
- queue_size¶
Maximum queue size that was exceeded.
- dropped_count¶
Number of messages dropped due to queue overflow.
Raised when message queue is full:
Consumer too slow
Burst of messages
Attributes:
queue_size: Maximum queue size exceededdropped_count: Number of messages dropped
Usage Examples¶
from kstlib.websocket import (
WebSocketError,
WebSocketConnectionError,
WebSocketClosedError,
WebSocketTimeoutError,
WebSocketReconnectError,
)
try:
async with WebSocketManager(url) as ws:
async for message in ws.stream():
process(message)
except WebSocketConnectionError as e:
log.error(f"Failed to connect to {e.url} after {e.attempts} attempts")
except WebSocketClosedError as e:
log.warning(f"Connection closed: {e.code} - {e.reason}")
except WebSocketTimeoutError as e:
log.warning(f"Timeout during {e.operation}: {e.timeout}s")
except WebSocketReconnectError as e:
log.error(f"Reconnection failed after {e.attempts} attempts")
except WebSocketError as e:
log.error(f"WebSocket error: {e}")