mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-25 16:46:35 +01:00
catch exceptions in error handlerfor errors that happen during polling (2) (#810)
* catch exceptions in error handlerfor errors that happen during polling * add tests for error handlers that raise exceptions
This commit is contained in:
parent
eae139d3e9
commit
1f5311b473
2 changed files with 32 additions and 1 deletions
|
@ -258,8 +258,12 @@ class Dispatcher(object):
|
|||
"""
|
||||
# An error happened while polling
|
||||
if isinstance(update, TelegramError):
|
||||
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)):
|
||||
|
|
|
@ -47,6 +47,9 @@ class TestDispatcher(object):
|
|||
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 @@ class TestDispatcher(object):
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue