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

catch exceptions in error handlerfor errors that happen during polling (2) #810

Merged
merged 2 commits into from Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion telegram/ext/dispatcher.py
Expand Up @@ -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)):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_dispatcher.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down