diff options
Diffstat (limited to 'telepot')
| -rw-r--r-- | telepot/exception.py | 3 | ||||
| -rw-r--r-- | telepot/loop.py | 28 |
2 files changed, 20 insertions, 11 deletions
diff --git a/telepot/exception.py b/telepot/exception.py index 820a522..04104c3 100644 --- a/telepot/exception.py +++ b/telepot/exception.py @@ -62,6 +62,9 @@ class IdleTerminate(WaitTooLong): class StopListening(TelepotException): pass +class UpdateKeyError(TelepotException): + pass + class TelegramError(TelepotException): """ To indicate erroneous situations, Telegram returns a JSON object containing diff --git a/telepot/loop.py b/telepot/loop.py index 4b1331b..ad13491 100644 --- a/telepot/loop.py +++ b/telepot/loop.py @@ -64,7 +64,10 @@ class GetUpdatesLoop(RunForeverAsThread): # No sort. Trust server to give messages in correct order. for update in result: - self._update_handler(update) + try: + self._update_handler(update) + except exception.UpdateKeyError: + print(f"Update with id {update['update_id']} caused an error, skipping", file=sys.stderr) offset = update['update_id'] + 1 except exception.BadHTTPResponse as e: @@ -100,16 +103,19 @@ def _dictify27(data): _dictify = _dictify3 if sys.version_info >= (3,) else _dictify27 def _extract_message(update): - key = _find_first_key(update, ['message', - 'edited_message', - 'channel_post', - 'edited_channel_post', - 'callback_query', - 'inline_query', - 'chosen_inline_result', - 'shipping_query', - 'pre_checkout_query']) - return key, update[key] + try: + key = _find_first_key(update, ['message', + 'edited_message', + 'channel_post', + 'edited_channel_post', + 'callback_query', + 'inline_query', + 'chosen_inline_result', + 'shipping_query', + 'pre_checkout_query']) + return key, update[key] + except KeyError as ex: + raise exception.UpdateKeyError from ex def _infer_handler_function(bot, h): if h is None: |
