diff --git a/docs/server.rst b/docs/server.rst index 9261a425..1bc4c68a 100644 --- a/docs/server.rst +++ b/docs/server.rst @@ -246,6 +246,8 @@ a :class:`socketio.exceptions.ConnectionRefusedError` exception can be raised, and all of its arguments will be sent to the client with the rejection message:: + from socketio.exceptions import ConnectionRefusedError + @sio.event def connect(sid, environ, auth): raise ConnectionRefusedError('authentication failed') diff --git a/src/socketio/async_server.py b/src/socketio/async_server.py index fac0f2b0..6c9e3ca3 100644 --- a/src/socketio/async_server.py +++ b/src/socketio/async_server.py @@ -561,6 +561,9 @@ async def _handle_connect(self, eio_sid, namespace, data): except exceptions.ConnectionRefusedError as exc: fail_reason = exc.error_args success = False + except ConnectionRefusedError: + fail_reason = {"message": "Connection refused by server"} + success = False if success is False: if self.always_connect: diff --git a/src/socketio/server.py b/src/socketio/server.py index 71c702de..f3257081 100644 --- a/src/socketio/server.py +++ b/src/socketio/server.py @@ -543,6 +543,9 @@ def _handle_connect(self, eio_sid, namespace, data): except exceptions.ConnectionRefusedError as exc: fail_reason = exc.error_args success = False + except ConnectionRefusedError: + fail_reason = {"message": "Connection refused by server"} + success = False if success is False: if self.always_connect: diff --git a/tests/async/test_server.py b/tests/async/test_server.py index f60de27a..575f2097 100644 --- a/tests/async/test_server.py +++ b/tests/async/test_server.py @@ -482,6 +482,21 @@ async def test_handle_connect_rejected_with_exception(self, eio): '123', '4{"message":"fail_reason"}') assert s.environ == {'123': 'environ'} + async def test_handle_connect_rejected_with_python_exception(self, eio): + eio.return_value.send = mock.AsyncMock() + s = async_server.AsyncServer() + handler = mock.MagicMock( + side_effect=ConnectionRefusedError() + ) + s.on('connect', handler) + await s._handle_eio_connect('123', 'environ') + await s._handle_eio_message('123', '0') + assert not s.manager.is_connected('1', '/') + handler.assert_called_once_with('1', 'environ') + s.eio.send.assert_awaited_once_with( + '123', '4{"message":"Connection refused by server"}') + assert s.environ == {'123': 'environ'} + async def test_handle_connect_rejected_with_empty_exception(self, eio): eio.return_value.send = mock.AsyncMock() s = async_server.AsyncServer() diff --git a/tests/common/test_server.py b/tests/common/test_server.py index 445d5d9e..bdbbfe07 100644 --- a/tests/common/test_server.py +++ b/tests/common/test_server.py @@ -462,6 +462,20 @@ def test_handle_connect_rejected_with_exception(self, eio): s.eio.send.assert_called_once_with('123', '4{"message":"fail_reason"}') assert s.environ == {'123': 'environ'} + def test_handle_connect_rejected_with_python_exception(self, eio): + s = server.Server() + handler = mock.MagicMock( + side_effect=ConnectionRefusedError() + ) + s.on('connect', handler) + s._handle_eio_connect('123', 'environ') + s._handle_eio_message('123', '0') + assert not s.manager.is_connected('1', '/') + handler.assert_called_once_with('1', 'environ') + s.eio.send.assert_called_once_with( + '123', '4{"message":"Connection refused by server"}') + assert s.environ == {'123': 'environ'} + def test_handle_connect_rejected_with_empty_exception(self, eio): s = server.Server() handler = mock.MagicMock(