From 98bed6f01ad922c2782ded17871bfbb50ecbcd18 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sat, 6 Jul 2024 16:09:04 +0200 Subject: [PATCH] Log Received Data on Deserialization Errors (#4304) --- telegram/_bot.py | 11 ++++++++++- telegram/ext/_utils/webhookhandler.py | 3 ++- tests/ext/test_updater.py | 1 + tests/test_bot.py | 23 +++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index 85cf41791..81b75419b 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -4374,7 +4374,16 @@ class Bot(TelegramObject, AsyncContextManager["Bot"]): else: self._LOGGER.debug("No new updates found.") - return Update.de_list(result, self) + try: + return Update.de_list(result, self) + except Exception as exc: + # This logging is in place mostly b/c we can't access the raw json data in Updater, + # where the exception is caught and logged again. Still, it might also be beneficial + # for custom usages of `get_updates`. + self._LOGGER.critical( + "Error while parsing updates! Received data was %r", result, exc_info=exc + ) + raise exc async def set_webhook( self, diff --git a/telegram/ext/_utils/webhookhandler.py b/telegram/ext/_utils/webhookhandler.py index 828dbca47..a174fbaa4 100644 --- a/telegram/ext/_utils/webhookhandler.py +++ b/telegram/ext/_utils/webhookhandler.py @@ -168,7 +168,8 @@ class TelegramHandler(tornado.web.RequestHandler): except Exception as exc: _LOGGER.critical( "Something went wrong processing the data received from Telegram. " - "Received data was *not* processed!", + "Received data was *not* processed! Received data was: %r", + data, exc_info=exc, ) raise tornado.web.HTTPError( diff --git a/tests/ext/test_updater.py b/tests/ext/test_updater.py index feed189c6..1fd8985de 100644 --- a/tests/ext/test_updater.py +++ b/tests/ext/test_updater.py @@ -1179,6 +1179,7 @@ class TestUpdater: assert len(caplog.records) == 1 assert caplog.records[-1].getMessage().startswith("Something went wrong processing") + assert "Received data was: {" in caplog.records[-1].getMessage() assert caplog.records[-1].name == "telegram.ext.Updater" assert response.status_code == 400 assert response.text == self.response_text.format( diff --git a/tests/test_bot.py b/tests/test_bot.py index 05d655450..df142e3fb 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -535,6 +535,29 @@ class TestBotWithoutRequest: 123, "text", api_kwargs={"unknown_kwarg_1": 7, "unknown_kwarg_2": 5} ) + async def test_get_updates_deserialization_error(self, bot, monkeypatch, caplog): + async def faulty_do_request(*args, **kwargs): + return ( + HTTPStatus.OK, + b'{"ok": true, "result": [{"update_id": "1", "message": "unknown_format"}]}', + ) + + monkeypatch.setattr(HTTPXRequest, "do_request", faulty_do_request) + + bot = PytestExtBot(get_updates_request=HTTPXRequest(), token=bot.token) + + with caplog.at_level(logging.CRITICAL), pytest.raises(AttributeError): + await bot.get_updates() + + assert len(caplog.records) == 1 + assert caplog.records[0].name == "telegram.ext.ExtBot" + assert caplog.records[0].levelno == logging.CRITICAL + assert caplog.records[0].getMessage() == ( + "Error while parsing updates! Received data was " + "[{'update_id': '1', 'message': 'unknown_format'}]" + ) + assert caplog.records[0].exc_info[0] is AttributeError + async def test_answer_web_app_query(self, bot, raw_bot, monkeypatch): params = False