Skip to content

Commit

Permalink
Connection: Handle websocket exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkn committed Jul 22, 2023
1 parent bf6c0b5 commit 5bfde11
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "xapi-python"
version = "0.1.3"
version = "0.1.4"
authors = [
{ name="Paweł Knioła", email="[email protected]" },
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"Programming Language :: Python :: 3.12"
],
python_requires='>=3.7',
version="0.1.3",
version="0.1.4",
packages=['xapi'],
)
8 changes: 8 additions & 0 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

class TestConnection(unittest.IsolatedAsyncioTestCase):

async def test_connect_websocket_exception(self):
c = Connection()
with patch("websockets.client.connect", new_callable=AsyncMock) as mocked_connect:
mocked_connect.side_effect = websockets.exceptions.WebSocketException()
with self.assertRaises(ConnectionClosed) as cm:
await c.connect("ws://127.0.0.1:9000")
self.assertEqual(str(cm.exception), "WebSocket exception: ")

async def test_connect_socket_gaierror(self):
c = Connection()
with patch("websockets.client.connect", new_callable=AsyncMock) as mocked_connect:
Expand Down
2 changes: 1 addition & 1 deletion xapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

name = "xapi"
__version__ = "0.1.3"
__version__ = "0.1.4"

from .xapi import XAPI, connect
from .enums import TradeCmd, TradeType, TradeStatus, PeriodCode
Expand Down
3 changes: 3 additions & 0 deletions xapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ async def connect(self, url):
try:
self._conn = await websockets.client.connect(url, close_timeout=0, max_size=None)

except websockets.exceptions.WebSocketException as e:
raise ConnectionClosed(f"WebSocket exception: {e}")

except socket.gaierror:
raise ConnectionClosed("Hostname cannot be resolved")

Expand Down

0 comments on commit 5bfde11

Please sign in to comment.