diff --git a/telegram/request/_baserequest.py b/telegram/request/_baserequest.py index 6c45bbf70..7e24c39ad 100644 --- a/telegram/request/_baserequest.py +++ b/telegram/request/_baserequest.py @@ -26,6 +26,7 @@ from typing import AsyncContextManager, ClassVar, List, Optional, Tuple, Type, T from telegram._utils.defaultvalue import DEFAULT_NONE as _DEFAULT_NONE from telegram._utils.defaultvalue import DefaultValue +from telegram._utils.logging import get_logger from telegram._utils.types import JSONDict, ODVInput from telegram._version import __version__ as ptb_ver from telegram.error import ( @@ -42,6 +43,8 @@ from telegram.request._requestdata import RequestData RT = TypeVar("RT", bound="BaseRequest") +_LOGGER = get_logger(__name__, class_name="BaseRequest") + class BaseRequest( AsyncContextManager["BaseRequest"], @@ -351,6 +354,7 @@ class BaseRequest( try: return json.loads(decoded_s) except ValueError as exc: + _LOGGER.error('Can not load invalid JSON data: "%s"', decoded_s) raise TelegramError("Invalid server response") from exc @abc.abstractmethod diff --git a/tests/request/test_request.py b/tests/request/test_request.py index e2b3b066d..9203e4336 100644 --- a/tests/request/test_request.py +++ b/tests/request/test_request.py @@ -20,6 +20,7 @@ implementations for BaseRequest and we want to test HTTPXRequest anyway.""" import asyncio import json +import logging from collections import defaultdict from dataclasses import dataclass from http import HTTPStatus @@ -172,15 +173,22 @@ class TestRequestWithoutRequest: # not only implicitly. assert httpx_request.parse_json_payload(server_response) == {"result": "test_string�"} - async def test_illegal_json_response(self, monkeypatch, httpx_request: HTTPXRequest): + async def test_illegal_json_response(self, monkeypatch, httpx_request: HTTPXRequest, caplog): # for proper JSON it should be `"result":` instead of `result:` server_response = b'{result: "test_string"}' monkeypatch.setattr(httpx_request, "do_request", mocker_factory(response=server_response)) - with pytest.raises(TelegramError, match="Invalid server response"): + with pytest.raises(TelegramError, match="Invalid server response"), caplog.at_level( + logging.ERROR + ): await httpx_request.post(None, None, None) + assert len(caplog.records) == 1 + record = caplog.records[0] + assert record.name == "telegram.request.BaseRequest" + assert record.getMessage().endswith(f'invalid JSON data: "{server_response.decode()}"') + async def test_chat_migrated(self, monkeypatch, httpx_request: HTTPXRequest): server_response = b'{"ok": "False", "parameters": {"migrate_to_chat_id": 123}}'