diff --git a/zerver/lib/event_queue.py b/zerver/lib/event_queue.py index b80fcad1da..938e34d17a 100644 --- a/zerver/lib/event_queue.py +++ b/zerver/lib/event_queue.py @@ -469,13 +469,14 @@ def fetch_events(query): was_connected = client.finish_current_handler() if not client.event_queue.empty() or dont_block: - ret = {'events': client.event_queue.contents()} + response = dict(events=client.event_queue.contents(), + handler_id=handler_id) if orig_queue_id is None: - ret['queue_id'] = queue_id - extra_log_data = "[%s/%s]" % (queue_id, len(ret["events"])) + response['queue_id'] = queue_id + extra_log_data = "[%s/%s]" % (queue_id, len(response["events"])) if was_connected: extra_log_data += " [was connected]" - return (ret, extra_log_data) + return dict(type="response", response=response, extra_log_data=extra_log_data) # After this point, dont_block=False, the queue is empty, and we # have a pre-existing queue, so we wait for new events. @@ -483,10 +484,13 @@ def fetch_events(query): logging.info("Disconnected handler for queue %s (%s/%s)" % (queue_id, user_profile_email, client_type_name)) except JsonableError as e: + if hasattr(e, 'to_json_error_msg') and callable(e.to_json_error_msg): + return dict(type="error", handler_id=handler_id, + message=e.to_json_error_msg()) raise e client.connect_handler(handler_id, client_type_name) - return (RespondAsynchronously, None) + return dict(type="async") # The following functions are called from Django diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py index 87780f5090..d40db5c6ed 100644 --- a/zerver/lib/test_helpers.py +++ b/zerver/lib/test_helpers.py @@ -3,6 +3,7 @@ from django.test import TestCase from zerver.lib.initial_password import initial_password from zerver.lib.db import TimeTrackingCursor +from zerver.lib.handlers import allocate_handler_id from zerver.lib import cache from zerver.lib import event_queue from zerver.worker import queue_processors diff --git a/zerver/tornadoviews.py b/zerver/tornadoviews.py index 8db02509c3..2386652642 100644 --- a/zerver/tornadoviews.py +++ b/zerver/tornadoviews.py @@ -82,9 +82,13 @@ def get_events_backend(request, user_profile, handler, last_connection_time = time.time(), narrow = narrow) - (result, log_data) = fetch_events(events_query) - request._log_data['extra'] = log_data - if result == RespondAsynchronously: + result = fetch_events(events_query) + if "extra_log_data" in result: + request._log_data['extra'] = result["extra_log_data"] + + if result["type"] == "async": handler._request = request - return result - return json_success(result) + return RespondAsynchronously + if result["type"] == "error": + return json_error(result["message"]) + return json_success(result["response"])