Skip to content

Error handlers not working with @run_async #682

@s-chizhik

Description

@s-chizhik

Steps to reproduce

  1. Register any handler, for ex: CommandHandler;
  2. Write some code to get TelegramException (ex: invalid Markdown/HTML);
  3. Register error handler;
  4. Run it.

Example code:

import logging

from telegram import Update, Bot, ParseMode
from telegram.ext import Updater, CommandHandler
from telegram.ext.dispatcher import run_async

updater = Updater('<telegram_token>')

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)
logger = logging.getLogger(__name__)

dispatcher = updater.dispatcher

msg = "<invalid_tag>Bold line"


def action_start(bot: Bot, update: Update):
    logger.info('/start')
    update.message.reply_text(msg, parse_mode=ParseMode.HTML)


@run_async
def action_async_start(bot: Bot, update: Update):
    logger.info('/async_start')
    update.message.reply_text(msg, parse_mode=ParseMode.HTML)


def _error(bot: Bot, update: Update, e: BaseException):
    logger.error("Boom!")


dp = updater.dispatcher

dp.add_handler(CommandHandler('start', action_start))
dp.add_handler(CommandHandler('async_start', action_async_start))

dp.add_error_handler(_error)

updater.start_polling()
updater.idle()

Expected behavior

Error handler will catch TelegramException, so handler could manage it.

Actual behavior

Error handler is not called.

Configuration

Version of Python, python-telegram-bot & dependencies:

python-telegram-bot 6.0.2
urllib3 1.21.1
certifi 2017.04.17
future 0.16.0
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

Logs

Commands to send:

/start
/async_start

Output:

<project_path>/venv/bin/python <project_path>/issue.py
2017-06-20 23:24:11,743 - __main__ - INFO - /start
2017-06-20 23:24:11,906 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update.
2017-06-20 23:24:11,907 - __main__ - ERROR - Boom!
2017-06-20 23:24:11,907 - __main__ - INFO - /async_start
2017-06-20 23:24:12,188 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
Traceback (most recent call last):
  File "<project_path>/venv/src/python-telegram-bot/telegram/utils/promise.py", line 42, in run
    self._result = self.pooled_function(*self.args, **self.kwargs)
  File "<project_path>/issue.py", line 25, in action_async_start
    update.message.reply_text(msg, parse_mode=ParseMode.HTML)
  File "<project_path>/venv/src/python-telegram-bot/telegram/message.py", line 340, in reply_text
    return self.bot.send_message(self.chat_id, *args, **kwargs)
  File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 125, in decorator
    result = func(self, *args, **kwargs)
  File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 158, in decorator
    return Bot._message_wrapper(self, url, data, *args, **kwargs)
  File "<project_path>/venv/src/python-telegram-bot/telegram/bot.py", line 146, in _message_wrapper
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
  File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 252, in post
    **urlopen_kwargs)
  File "<project_path>/venv/src/python-telegram-bot/telegram/utils/request.py", line 194, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Can't parse entities in message text: unsupported start tag "invalid_tag" at byte offset 0

Process finished with exit code 0

I suppose, dispatcher.dispatch_error should be called in the same thread with async handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions