catch BaseException in command_handler

This commit is contained in:
gcarq 2017-11-07 22:27:16 +01:00
parent 32327c45c2
commit 18eec0f4d4

View File

@ -92,15 +92,17 @@ def authorized_only(command_handler: Callable[[Bot, Update], None]) -> Callable[
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
bot, update = kwargs.get('bot') or args[0], kwargs.get('update') or args[1] bot, update = kwargs.get('bot') or args[0], kwargs.get('update') or args[1]
if not isinstance(bot, Bot) or not isinstance(update, Update): # Reject unauthorized messages
raise ValueError('Received invalid Arguments: {}'.format(*args))
chat_id = int(_CONF['telegram']['chat_id']) chat_id = int(_CONF['telegram']['chat_id'])
if int(update.message.chat_id) == chat_id: if int(update.message.chat_id) != chat_id:
logger.info('Executing handler: %s for chat_id: %s', command_handler.__name__, chat_id)
return command_handler(*args, **kwargs)
else:
logger.info('Rejected unauthorized message from: %s', update.message.chat_id) logger.info('Rejected unauthorized message from: %s', update.message.chat_id)
return wrapper
logger.info('Executing handler: %s for chat_id: %s', command_handler.__name__, chat_id)
try:
return command_handler(*args, **kwargs)
except BaseException:
logger.exception('Exception occurred within Telegram module')
return wrapper return wrapper
@ -447,17 +449,15 @@ def send_msg(msg: str, bot: Bot = None, parse_mode: ParseMode = ParseMode.MARKDO
""" """
if not is_enabled(): if not is_enabled():
return return
bot = bot or _UPDATER.bot
try: try:
bot = bot or _UPDATER.bot bot.send_message(_CONF['telegram']['chat_id'], msg, parse_mode=parse_mode)
try: except NetworkError as error:
bot.send_message(_CONF['telegram']['chat_id'], msg, parse_mode=parse_mode) # Sometimes the telegram server resets the current connection,
except NetworkError as error: # if this is the case we send the message again.
# Sometimes the telegram server resets the current connection, logger.warning(
# if this is the case we send the message again. 'Got Telegram NetworkError: %s! Trying one more time.',
logger.warning( error.message
'Got Telegram NetworkError: %s! Trying one more time.', )
error.message bot.send_message(_CONF['telegram']['chat_id'], msg, parse_mode=parse_mode)
)
bot.send_message(_CONF['telegram']['chat_id'], msg, parse_mode=parse_mode)
except BaseException:
logger.exception('Exception occurred within Telegram API')