Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log Received Data on Deserialization Errors #4304

Merged
merged 4 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4339,7 +4339,16 @@ async def get_updates(
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,
Expand Down
3 changes: 2 additions & 1 deletion telegram/ext/_utils/webhookhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ async def post(self) -> None:
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(
Expand Down
1 change: 1 addition & 0 deletions tests/ext/test_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ def de_json_fails(*args, **kwargs):

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(
Expand Down
23 changes: 23 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,29 @@ async def post(url, request_data: RequestData, *args, **kwargs):
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()
Bibo-Joshi marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
Loading