Skip to content

Commit 38edd2c

Browse files
committed
Add ConnectionRefusedError and handling for it
1 parent a8b13fc commit 38edd2c

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

socketio/asyncio_server.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import engineio
44

55
from . import asyncio_manager
6+
from . import exceptions
67
from . import packet
78
from . import server
89

@@ -320,11 +321,18 @@ async def _handle_connect(self, sid, namespace):
320321
"""Handle a client connection request."""
321322
namespace = namespace or '/'
322323
self.manager.connect(sid, namespace)
323-
if await self._trigger_event('connect', namespace, sid,
324-
self.environ[sid]) is False:
324+
325+
try:
326+
success = await self._trigger_event('connect', namespace, sid, self.environ[sid])
327+
except exceptions.ConnectionRefusedError as exc:
328+
fail_reason = exc.get_info()
329+
success = False
330+
else:
331+
fail_reason = None
332+
333+
if success is False:
325334
self.manager.disconnect(sid, namespace)
326-
await self._send_packet(sid, packet.Packet(packet.ERROR,
327-
namespace=namespace))
335+
await self._send_packet(sid, packet.Packet(packet.ERROR, data=fail_reason, namespace=namespace))
328336
if sid in self.environ: # pragma: no cover
329337
del self.environ[sid]
330338
return False

socketio/exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,17 @@ class SocketIOError(Exception):
44

55
class ConnectionError(SocketIOError):
66
pass
7+
8+
9+
class ConnectionRefusedError(ConnectionError):
10+
"""
11+
Raised when connection is refused on the application level
12+
"""
13+
def __init__(self, info):
14+
self._info = info
15+
16+
def get_info(self):
17+
"""
18+
This method could be overridden in subclass to add extra logic for data output
19+
"""
20+
return self._info

socketio/server.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import six
55

66
from . import base_manager
7+
from . import exceptions
78
from . import packet
89
from . import namespace
910

@@ -485,11 +486,17 @@ def _handle_connect(self, sid, namespace):
485486
"""Handle a client connection request."""
486487
namespace = namespace or '/'
487488
self.manager.connect(sid, namespace)
488-
if self._trigger_event('connect', namespace, sid,
489-
self.environ[sid]) is False:
489+
try:
490+
success = self._trigger_event('connect', namespace, sid, self.environ[sid])
491+
except exceptions.ConnectionRefusedError as exc:
492+
fail_reason = exc.get_info()
493+
success = False
494+
else:
495+
fail_reason = None
496+
497+
if success is False:
490498
self.manager.disconnect(sid, namespace)
491-
self._send_packet(sid, packet.Packet(packet.ERROR,
492-
namespace=namespace))
499+
self._send_packet(sid, packet.Packet(packet.ERROR, data=fail_reason, namespace=namespace))
493500
if sid in self.environ: # pragma: no cover
494501
del self.environ[sid]
495502
return False

0 commit comments

Comments
 (0)