Skip to content

Commit

Permalink
Add better exception handling for errors thrown by the websocket-clie… (
Browse files Browse the repository at this point in the history
#155)

* Add better exception handling for errors thrown by the websocket-client library.

* Add test for handling websocket errors.
  • Loading branch information
harmon authored and miguelgrinberg committed Dec 13, 2019
1 parent 02a2c70 commit 33c7cf1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion engineio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def _connect_websocket(self, url, headers, engineio_path):
ws = websocket.create_connection(
websocket_url + self._get_url_timestamp(), header=headers,
cookie=cookies)
except (ConnectionError, IOError):
except (ConnectionError, IOError, websocket.WebSocketException):
if upgrade:
self.logger.warning(
'WebSocket upgrade failed: connection error')
Expand Down
12 changes: 12 additions & 0 deletions tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ def test_websocket_connection_failed(self, create_connection, _time):
'ws://foo/engine.io/?transport=websocket&EIO=3&t=123.456',
header={'Foo': 'Bar'}, cookie=None)

@mock.patch('engineio.client.time.time', return_value=123.456)
@mock.patch('engineio.client.websocket.create_connection',
side_effect=[websocket.WebSocketException])
def test_websocket_connection_failed_with_websocket_error(
self, create_connection, _time):
c = client.Client()
self.assertRaises(exceptions.ConnectionError, c.connect, 'http://foo',
transports=['websocket'], headers={'Foo': 'Bar'})
create_connection.assert_called_once_with(
'ws://foo/engine.io/?transport=websocket&EIO=3&t=123.456',
header={'Foo': 'Bar'}, cookie=None)

@mock.patch('engineio.client.time.time', return_value=123.456)
@mock.patch('engineio.client.websocket.create_connection',
side_effect=[ConnectionError])
Expand Down

0 comments on commit 33c7cf1

Please sign in to comment.