From e7becef0b3ecbf5e100c7de35fdb29d4dc81b3f4 Mon Sep 17 00:00:00 2001 From: Smirnov Danil Date: Thu, 9 Jan 2020 17:17:50 +0300 Subject: [PATCH] HH-102457 Handle exceptions in futures --- frontik/futures.py | 9 +++++++-- tests/projects/test_app/pages/error_yield.py | 17 +++++++++++++++++ tests/test_yield_errors.py | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/projects/test_app/pages/error_yield.py create mode 100644 tests/test_yield_errors.py diff --git a/frontik/futures.py b/frontik/futures.py index a59a75815..734a0e0ef 100644 --- a/frontik/futures.py +++ b/frontik/futures.py @@ -1,6 +1,6 @@ import time import logging -from functools import wraps +from functools import wraps, partial from tornado.ioloop import IOLoop from tornado.concurrent import Future @@ -102,8 +102,13 @@ def new_cb(*args, **kwargs): return new_cb + @staticmethod + def _handle_future(callback, future): + future.result() + callback() + def add_future(self, future): - IOLoop.current().add_future(future, self.add_notification()) + IOLoop.current().add_future(future, partial(self._handle_future, self.add_notification())) return future def get_finish_future(self): diff --git a/tests/projects/test_app/pages/error_yield.py b/tests/projects/test_app/pages/error_yield.py new file mode 100644 index 000000000..97cb84583 --- /dev/null +++ b/tests/projects/test_app/pages/error_yield.py @@ -0,0 +1,17 @@ +from tornado import gen + +import frontik.handler + + +@gen.coroutine +def some_async_function(handler): + yield handler.post_url(handler.request.host, handler.request.path) + return 1 / 0 + + +class Page(frontik.handler.PageHandler): + def get_page(self): + self.finish_group.add_future(some_async_function(self)) + + def post_page(self): + self.text = 'result' diff --git a/tests/test_yield_errors.py b/tests/test_yield_errors.py new file mode 100644 index 000000000..0c19ad52a --- /dev/null +++ b/tests/test_yield_errors.py @@ -0,0 +1,9 @@ +import unittest + +from .instances import frontik_test_app + + +class TestHandler(unittest.TestCase): + def test_error_in_yield(self): + response = frontik_test_app.get_page('error_yield') + self.assertEqual(response.status_code, 500)