Skip to content

Commit

Permalink
Add a top-level exception class
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 10, 2024
1 parent d4f05c7 commit e7c225e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/simple_websocket/errors.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from wsproto.frame_protocol import CloseReason


class ConnectionError(RuntimeError): # pragma: no cover
class SimpleWebsocketError(RuntimeError):
pass


class ConnectionError(SimpleWebsocketError):
"""Connection error exception class."""
def __init__(self, status_code=None):
def __init__(self, status_code=None): # pragma: no cover
self.status_code = status_code
super().__init__(f'Connection error: {status_code}')


class ConnectionClosed(RuntimeError):
class ConnectionClosed(SimpleWebsocketError):
"""Connection closed exception class."""
def __init__(self, reason=CloseReason.NO_STATUS_RCVD, message=None):
self.reason = reason
Expand Down
15 changes: 6 additions & 9 deletions src/simple_websocket/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,21 +397,18 @@ def __init__(self, url, subprotocols=None, headers=None,
elif isinstance(headers, list):
self.extra_headeers = headers

connection_args = None
for conn in socket.getaddrinfo(self.host, self.port): # pragma: no branch
if conn[1] == socket.SOCK_STREAM: # pragma: no branch
connection_args = conn
break
if connection_args is None: # pragma: no cover
connection_args = socket.getaddrinfo(self.host, self.port,
type=socket.SOCK_STREAM)
if len(connection_args) == 0: # pragma: no cover
raise ConnectionError()
sock = socket.socket(connection_args[0], connection_args[1],
connection_args[2])
sock = socket.socket(connection_args[0][0], connection_args[0][1],
connection_args[0][2])
if is_secure: # pragma: no cover
if ssl_context is None:
ssl_context = ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH)
sock = ssl_context.wrap_socket(sock, server_hostname=self.host)
sock.connect(connection_args[4])
sock.connect(connection_args[0][4])
super().__init__(sock, connection_type=ConnectionType.CLIENT,
receive_bytes=receive_bytes,
ping_interval=ping_interval,
Expand Down

0 comments on commit e7c225e

Please sign in to comment.