Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Class based namespaces error handling #834

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ def on_namespace(self, namespace_handler):
else:
self.namespace_handlers.append(namespace_handler)

# if the namespace handler has an on_error method then register it
error_handler = getattr(namespace_handler, 'on_error', None)
if error_handler is not None:
self.on_error(namespace_handler.namespace)(error_handler)

def emit(self, event, *args, **kwargs):
"""Emit a server generated SocketIO event.

Expand Down
20 changes: 20 additions & 0 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ def on_other_custom_event(self, data):
request_event_data = request.event
emit('my custom response', data)

def on_error_testing(self, data):
raise AssertionError()

def on_error(self, value):
if isinstance(value, AssertionError):
global error_testing_namespace_class_based
error_testing_namespace_class_based = True
else:
raise value
return value



socketio.on_namespace(MyNamespace('/ns'))

Expand Down Expand Up @@ -615,6 +627,14 @@ def test_request_event_data_class_based(self):
expected_data = {'message': 'other_custom_event', 'args': ('foo',)}
self.assertEqual(request_event_data, expected_data)

def test_error_handling_class_based(self):
client = socketio.test_client(app, namespace='/ns')
client.get_received('/ns')
global error_testing_namespace_class_based
error_testing_namespace_class_based = False
client.emit('error_testing', 'testing', namespace='/ns')
self.assertTrue(error_testing_namespace_class_based)

def test_delayed_init(self):
app = Flask(__name__)
socketio = SocketIO(allow_upgrades=False, json=flask_json)
Expand Down