Python-telegram-bot: Error handlers not working with @run_async

Created on 20 Jun 2017  路  4Comments  路  Source: python-telegram-bot/python-telegram-bot

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.

bug

Most helpful comment

same problem, still not fix?

All 4 comments

Hi there,

Thanks for the report. We'll look into this.

same problem, still not fix?

Guys! I still have a problem! After I added

from telegram.utils import request
if request.is_con_pool_initialized():
    raise RuntimeError('this is not prior to anything else...')
request.CON_POOL_SIZE = 20

I got the error:

Traceback (most recent call last):
  File "bot.py", line 1, in <module>
    from frontend.tgbot.main_functions import main
  File "------------------------/frontend/tgbot/main_functions.py", line 35, in <module>
    if request.is_con_pool_initialized():
AttributeError: module 'telegram.utils.request' has no attribute 'is_con_pool_initialized'

What should I do now?

@ohld, you are probably using a quite old guide (or docs) for python-telegram-bot, this function was removed for more then 2 years now
aaand, probably, miss the topic

Was this page helpful?
1 / 5 - 1 ratings