Skip to content

Commit

Permalink
Merge 5239f52 into 16a49ec
Browse files Browse the repository at this point in the history
  • Loading branch information
jh0ker committed Aug 26, 2017
2 parents 16a49ec + 5239f52 commit 6a86894
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 6a86894

Please sign in to comment.