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 5bfde11 commit 1ea1e6e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 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.4"
version = "0.1.5"
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.4",
version="0.1.5",
packages=['xapi'],
)
11 changes: 6 additions & 5 deletions tests/test_connect.py → tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import websockets.client
import websockets.exceptions
import websockets.datastructures
import socket
import json
import asyncio
Expand All @@ -14,10 +15,10 @@ 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()
mocked_connect.side_effect = websockets.exceptions.InvalidStatusCode(status_code=404, headers=websockets.datastructures.Headers())
with self.assertRaises(ConnectionClosed) as cm:
await c.connect("ws://127.0.0.1:9000")
self.assertEqual(str(cm.exception), "WebSocket exception: ")
self.assertEqual(str(cm.exception), "WebSocket exception: server rejected WebSocket connection: HTTP 404")

async def test_connect_socket_gaierror(self):
c = Connection()
Expand Down Expand Up @@ -77,7 +78,7 @@ async def test_listen_connection_closed(self):
c._conn.__aiter__.side_effect = websockets.exceptions.ConnectionClosed(None, None)
with self.assertRaises(ConnectionClosed) as cm:
async for _ in c.listen(): pass
self.assertEqual(str(cm.exception), "Connection unexpectedly closed")
self.assertEqual(str(cm.exception), "WebSocket exception: no close frame received or sent")

async def test_request_with_connection(self):
conn = Connection()
Expand All @@ -100,7 +101,7 @@ async def test_request_connection_closed(self):
command = {"command": "test"}
with self.assertRaises(ConnectionClosed) as cm:
await conn._request(command)
self.assertEqual(str(cm.exception), "Connection unexpectedly closed")
self.assertEqual(str(cm.exception), "WebSocket exception: no close frame received or sent")

async def test_transaction_with_connection(self):
conn = Connection()
Expand All @@ -127,4 +128,4 @@ async def test_transaction_connection_closed(self):
command = {"command": "test"}
with self.assertRaises(ConnectionClosed) as cm:
await conn._transaction(command)
self.assertEqual(str(cm.exception), "Connection unexpectedly closed")
self.assertEqual(str(cm.exception), "WebSocket exception: no close frame received or sent")
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.4"
__version__ = "0.1.5"

from .xapi import XAPI, connect
from .enums import TradeCmd, TradeType, TradeStatus, PeriodCode
Expand Down
12 changes: 6 additions & 6 deletions xapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ async def listen(self):
else:
raise ConnectionClosed("Not connected")

except websockets.exceptions.ConnectionClosed:
except websockets.exceptions.WebSocketException as e:
self._conn = None
raise ConnectionClosed("Connection unexpectedly closed")
raise ConnectionClosed(f"WebSocket exception: {e}")

async def _request(self, command):
try:
Expand All @@ -57,9 +57,9 @@ async def _request(self, command):
else:
raise ConnectionClosed("Not connected")

except websockets.exceptions.ConnectionClosed:
except websockets.exceptions.WebSocketException as e:
self._conn = None
raise ConnectionClosed("Connection unexpectedly closed")
raise ConnectionClosed(f"WebSocket exception: {e}")

async def _transaction(self, command):
async with self._lock:
Expand All @@ -71,6 +71,6 @@ async def _transaction(self, command):
else:
raise ConnectionClosed("Not connected")

except websockets.exceptions.ConnectionClosed:
except websockets.exceptions.WebSocketException as e:
self._conn = None
raise ConnectionClosed("Connection unexpectedly closed")
raise ConnectionClosed(f"WebSocket exception: {e}")

0 comments on commit 1ea1e6e

Please sign in to comment.