diff --git a/telegram/ext/dispatcher.py b/telegram/ext/dispatcher.py index bd523fbe5a4..6453a6d48d0 100644 --- a/telegram/ext/dispatcher.py +++ b/telegram/ext/dispatcher.py @@ -264,8 +264,12 @@ def process_update(self, update): # An error happened while polling if isinstance(update, TelegramError): - self.dispatch_error(None, update) + try: + self.dispatch_error(None, update) + except Exception: + self.logger.exception('An uncaught error was raised while handling the error') return + for group in self.groups: try: for handler in (x for x in self.handlers[group] if x.check_update(update)): diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index 8ddf6077213..55ef5de801f 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -47,6 +47,9 @@ def reset(self): def error_handler(self, bot, update, error): self.received = error.message + def error_handler_raise_error(self, bot, update, error): + raise Exception('Failing bigly') + def callback_increase_count(self, bot, update): self.count += 1 @@ -78,6 +81,30 @@ def test_error_handler(self, dp): sleep(.1) assert self.received is None + def test_error_handler_that_raises_errors(self, dp): + """ + Make sure that errors raised in error handlers don't break the main loop of the dispatcher + """ + handler_raise_error = MessageHandler(Filters.all, self.callback_raise_error) + handler_increase_count = MessageHandler(Filters.all, self.callback_increase_count) + error = TelegramError('Unauthorized.') + + dp.add_error_handler(self.error_handler_raise_error) + + # From errors caused by handlers + dp.add_handler(handler_raise_error) + dp.update_queue.put(self.message_update) + sleep(.1) + + # From errors in the update_queue + dp.remove_handler(handler_raise_error) + dp.add_handler(handler_increase_count) + dp.update_queue.put(error) + dp.update_queue.put(self.message_update) + sleep(.1) + + assert self.count == 1 + def test_run_async_multiple(self, bot, dp, dp2): def get_dispatcher_name(q): q.put(current_thread().name)