Skip to content

Commit 314971c

Browse files
Include error message and arguments in CONNECT_ERROR packet (Fixes #590)
1 parent f0dddfc commit 314971c

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

socketio/asyncio_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ async def _handle_connect(self, eio_sid, namespace):
411411
if self.always_connect:
412412
await self._send_packet(eio_sid, packet.Packet(
413413
packet.CONNECT, {'sid': sid}, namespace=namespace))
414-
fail_reason = None
414+
fail_reason = exceptions.ConnectionRefusedError().error_args
415415
try:
416416
success = await self._trigger_event('connect', namespace, sid,
417417
self.environ[eio_sid])

socketio/exceptions.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ class ConnectionRefusedError(ConnectionError):
1515
"""
1616
def __init__(self, *args):
1717
if len(args) == 0:
18-
self.error_args = None
19-
elif len(args) == 1 and not isinstance(args[0], list):
20-
self.error_args = args[0]
18+
self.error_args = {'message': 'Connection rejected by server'}
19+
elif len(args) == 1:
20+
self.error_args = {'message': str(args[0])}
2121
else:
22-
self.error_args = args
22+
self.error_args = {'message': str(args[0])}
23+
if len(args) == 2:
24+
self.error_args['data'] = args[1]
25+
else:
26+
self.error_args['data'] = args[1:]
2327

2428

2529
class TimeoutError(SocketIOError):

socketio/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def _handle_connect(self, eio_sid, namespace):
618618
if self.always_connect:
619619
self._send_packet(eio_sid, packet.Packet(
620620
packet.CONNECT, {'sid': sid}, namespace=namespace))
621-
fail_reason = None
621+
fail_reason = exceptions.ConnectionRefusedError().error_args
622622
try:
623623
success = self._trigger_event('connect', namespace, sid,
624624
self.environ[eio_sid])

tests/asyncio/test_asyncio_server.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ def test_handle_connect_rejected(self, eio):
429429
_run(s._handle_eio_message('123', '0'))
430430
assert not s.manager.is_connected('1', '/foo')
431431
handler.assert_called_once_with('1', 'environ')
432-
s.eio.send.mock.assert_called_once_with('123', '4')
432+
s.eio.send.mock.assert_called_once_with(
433+
'123', '4{"message":"Connection rejected by server"}')
433434
assert s.environ == {'123': 'environ'}
434435

435436
def test_handle_connect_namespace_rejected(self, eio):
@@ -441,7 +442,8 @@ def test_handle_connect_namespace_rejected(self, eio):
441442
_run(s._handle_eio_message('123', '0/foo'))
442443
assert not s.manager.is_connected('1', '/foo')
443444
handler.assert_called_once_with('1', 'environ')
444-
s.eio.send.mock.assert_any_call('123', '4/foo')
445+
s.eio.send.mock.assert_any_call(
446+
'123', '4/foo,{"message":"Connection rejected by server"}')
445447
assert s.environ == {'123': 'environ'}
446448

447449
def test_handle_connect_rejected_always_connect(self, eio):
@@ -454,7 +456,8 @@ def test_handle_connect_rejected_always_connect(self, eio):
454456
assert not s.manager.is_connected('1', '/')
455457
handler.assert_called_once_with('1', 'environ')
456458
s.eio.send.mock.assert_any_call('123', '0{"sid":"1"}')
457-
s.eio.send.mock.assert_any_call('123', '1')
459+
s.eio.send.mock.assert_any_call(
460+
'123', '1{"message":"Connection rejected by server"}')
458461
assert s.environ == {'123': 'environ'}
459462

460463
def test_handle_connect_namespace_rejected_always_connect(self, eio):
@@ -467,7 +470,8 @@ def test_handle_connect_namespace_rejected_always_connect(self, eio):
467470
assert not s.manager.is_connected('1', '/foo')
468471
handler.assert_called_once_with('1', 'environ')
469472
s.eio.send.mock.assert_any_call('123', '0/foo,{"sid":"1"}')
470-
s.eio.send.mock.assert_any_call('123', '1/foo')
473+
s.eio.send.mock.assert_any_call(
474+
'123', '1/foo,{"message":"Connection rejected by server"}')
471475
assert s.environ == {'123': 'environ'}
472476

473477
def test_handle_connect_rejected_with_exception(self, eio):
@@ -481,7 +485,8 @@ def test_handle_connect_rejected_with_exception(self, eio):
481485
_run(s._handle_eio_message('123', '0'))
482486
assert not s.manager.is_connected('1', '/')
483487
handler.assert_called_once_with('1', 'environ')
484-
s.eio.send.mock.assert_called_once_with('123', '4"fail_reason"')
488+
s.eio.send.mock.assert_called_once_with(
489+
'123', '4{"message":"fail_reason"}')
485490
assert s.environ == {'123': 'environ'}
486491

487492
def test_handle_connect_rejected_with_empty_exception(self, eio):
@@ -495,22 +500,24 @@ def test_handle_connect_rejected_with_empty_exception(self, eio):
495500
_run(s._handle_eio_message('123', '0'))
496501
assert not s.manager.is_connected('1', '/')
497502
handler.assert_called_once_with('1', 'environ')
498-
s.eio.send.mock.assert_called_once_with('123', '4')
503+
s.eio.send.mock.assert_called_once_with(
504+
'123', '4{"message":"Connection rejected by server"}')
499505
assert s.environ == {'123': 'environ'}
500506

501507
def test_handle_connect_namespace_rejected_with_exception(self, eio):
502508
eio.return_value.send = AsyncMock()
503509
s = asyncio_server.AsyncServer()
504510
handler = mock.MagicMock(
505-
side_effect=exceptions.ConnectionRefusedError('fail_reason', 1)
511+
side_effect=exceptions.ConnectionRefusedError(
512+
'fail_reason', 1, '2')
506513
)
507514
s.on('connect', handler, namespace='/foo')
508515
_run(s._handle_eio_connect('123', 'environ'))
509516
_run(s._handle_eio_message('123', '0/foo'))
510517
assert not s.manager.is_connected('1', '/foo')
511518
handler.assert_called_once_with('1', 'environ')
512-
s.eio.send.mock.assert_called_once_with('123',
513-
'4/foo,["fail_reason",1]')
519+
s.eio.send.mock.assert_called_once_with(
520+
'123', '4/foo,{"message":"fail_reason","data":[1,"2"]}')
514521
assert s.environ == {'123': 'environ'}
515522

516523
def test_handle_connect_namespace_rejected_with_empty_exception(self, eio):
@@ -524,7 +531,8 @@ def test_handle_connect_namespace_rejected_with_empty_exception(self, eio):
524531
_run(s._handle_eio_message('123', '0/foo'))
525532
assert not s.manager.is_connected('1', '/foo')
526533
handler.assert_called_once_with('1', 'environ')
527-
s.eio.send.mock.assert_called_once_with('123', '4/foo')
534+
s.eio.send.mock.assert_called_once_with(
535+
'123', '4/foo,{"message":"Connection rejected by server"}')
528536
assert s.environ == {'123': 'environ'}
529537

530538
def test_handle_disconnect(self, eio):

tests/common/test_server.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ def test_handle_connect_rejected(self, eio):
359359
assert not s.manager.is_connected('1', '/')
360360
handler.assert_called_once_with('1', 'environ')
361361
assert not s.manager.is_connected('1', '/')
362-
s.eio.send.assert_called_once_with('123', '4')
362+
s.eio.send.assert_called_once_with(
363+
'123', '4{"message":"Connection rejected by server"}')
363364
assert s.environ == {'123': 'environ'}
364365

365366
def test_handle_connect_namespace_rejected(self, eio):
@@ -371,7 +372,8 @@ def test_handle_connect_namespace_rejected(self, eio):
371372
assert not s.manager.is_connected('1', '/foo')
372373
handler.assert_called_once_with('1', 'environ')
373374
assert not s.manager.is_connected('1', '/foo')
374-
s.eio.send.assert_called_once_with('123', '4/foo')
375+
s.eio.send.assert_called_once_with(
376+
'123', '4/foo,{"message":"Connection rejected by server"}')
375377
assert s.environ == {'123': 'environ'}
376378

377379
def test_handle_connect_rejected_always_connect(self, eio):
@@ -383,7 +385,8 @@ def test_handle_connect_rejected_always_connect(self, eio):
383385
assert not s.manager.is_connected('1', '/')
384386
handler.assert_called_once_with('1', 'environ')
385387
s.eio.send.assert_any_call('123', '0{"sid":"1"}')
386-
s.eio.send.assert_any_call('123', '1')
388+
s.eio.send.assert_any_call(
389+
'123', '1{"message":"Connection rejected by server"}')
387390
assert s.environ == {'123': 'environ'}
388391

389392
def test_handle_connect_namespace_rejected_always_connect(self, eio):
@@ -395,7 +398,8 @@ def test_handle_connect_namespace_rejected_always_connect(self, eio):
395398
assert not s.manager.is_connected('1', '/foo')
396399
handler.assert_called_once_with('1', 'environ')
397400
s.eio.send.assert_any_call('123', '0/foo,{"sid":"1"}')
398-
s.eio.send.assert_any_call('123', '1/foo')
401+
s.eio.send.assert_any_call(
402+
'123', '1/foo,{"message":"Connection rejected by server"}')
399403
assert s.environ == {'123': 'environ'}
400404

401405
def test_handle_connect_rejected_with_exception(self, eio):
@@ -408,7 +412,7 @@ def test_handle_connect_rejected_with_exception(self, eio):
408412
s._handle_eio_message('123', '0')
409413
assert not s.manager.is_connected('1', '/')
410414
handler.assert_called_once_with('1', 'environ')
411-
s.eio.send.assert_called_once_with('123', '4"fail_reason"')
415+
s.eio.send.assert_called_once_with('123', '4{"message":"fail_reason"}')
412416
assert s.environ == {'123': 'environ'}
413417

414418
def test_handle_connect_rejected_with_empty_exception(self, eio):
@@ -421,20 +425,21 @@ def test_handle_connect_rejected_with_empty_exception(self, eio):
421425
s._handle_eio_message('123', '0')
422426
assert not s.manager.is_connected('1', '/')
423427
handler.assert_called_once_with('1', 'environ')
424-
s.eio.send.assert_called_once_with('123', '4')
428+
s.eio.send.assert_called_once_with(
429+
'123', '4{"message":"Connection rejected by server"}')
425430
assert s.environ == {'123': 'environ'}
426431

427432
def test_handle_connect_namespace_rejected_with_exception(self, eio):
428433
s = server.Server()
429434
handler = mock.MagicMock(
430-
side_effect=exceptions.ConnectionRefusedError(u'fail_reason', 1)
435+
side_effect=exceptions.ConnectionRefusedError('fail_reason', 1)
431436
)
432437
s.on('connect', handler, namespace='/foo')
433438
s._handle_eio_connect('123', 'environ')
434439
s._handle_eio_message('123', '0/foo')
435440
assert not s.manager.is_connected('1', '/foo')
436441
s.eio.send.assert_called_once_with(
437-
'123', '4/foo,["fail_reason",1]'
442+
'123', '4/foo,{"message":"fail_reason","data":1}'
438443
)
439444
assert s.environ == {'123': 'environ'}
440445

@@ -447,7 +452,8 @@ def test_handle_connect_namespace_rejected_with_empty_exception(self, eio):
447452
s._handle_eio_connect('123', 'environ')
448453
s._handle_eio_message('123', '0/foo')
449454
assert not s.manager.is_connected('1', '/foo')
450-
s.eio.send.assert_called_once_with('123', '4/foo')
455+
s.eio.send.assert_called_once_with(
456+
'123', '4/foo,{"message":"Connection rejected by server"}')
451457
assert s.environ == {'123': 'environ'}
452458

453459
def test_handle_disconnect(self, eio):

0 commit comments

Comments
 (0)