mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
Add Logging for Invalid JSON Data in BasePersistence.parse_json_payload
(#3668)
This commit is contained in:
parent
7b116be344
commit
f23315d08b
2 changed files with 14 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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<EFBFBD>"}
|
||||
|
||||
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}}'
|
||||
|
||||
|
|
Loading…
Reference in a new issue