8000 Add Logging for Invalid JSON Data in `BasePersistence.parse_json_payl… · Devors/python-telegram-bot@f23315d · GitHub
[go: up one dir, main page]

Skip to content

Commit f23315d

Browse files
authored
Add Logging for Invalid JSON Data in BasePersistence.parse_json_payload (python-telegram-bot#3668)
1 parent 7b116be commit f23315d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

telegram/request/_baserequest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from telegram._utils.defaultvalue import DEFAULT_NONE as _DEFAULT_NONE
2828
from telegram._utils.defaultvalue import DefaultValue
29+
from telegram._utils.logging import get_logger
2930
from telegram._utils.types import JSONDict, ODVInput
3031
from telegram._version import __version__ as ptb_ver
3132
from telegram.error import (
@@ -42,6 +43,8 @@
4243

4344
RT = TypeVar("RT", bound="BaseRequest")
4445

46+
_LOGGER = get_logger(__name__, class_name="BaseRequest")
47+
< 10000 /td>
4548

4649
class BaseRequest(
4750
AsyncContextManager["BaseRequest"],
@@ -351,6 +354,7 @@ def parse_json_payload(payload: bytes) -> JSONDict:
351354
try:
352355
return json.loads(decoded_s)
353356
except ValueError as exc:
357+
_LOGGER.error('Can not load invalid JSON data: "%s"', decoded_s)
354358
raise TelegramError("Invalid server response") from exc
355359

356360
@abc.abstractmethod

tests/request/test_request.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
implementations for BaseRequest and we want to test HTTPXRequest anyway."""
2121
import asyncio
2222
import json
23+
import logging
2324
from collections import defaultdict
2425
from dataclasses import dataclass
2526
from http import HTTPStatus
@@ -172,15 +173,22 @@ async def test_replaced_unprintable_char(self, monkeypatch, httpx_request):
172173
# not only implicitly.
173174
assert httpx_request.parse_json_payload(server_response) == {"result": "test_string�"}
174175

175-
async def test_illegal_json_response(self, monkeypatch, httpx_request: HTTPXRequest):
176+
async def test_illegal_json_response(self, monkeypatch, httpx_request: HTTPXRequest, caplog):
176177
# for proper JSON it should be `"result":` instead of `result:`
177178
server_response = b'{result: "test_string"}'
178179

179180
monkeypatch.setattr(httpx_request, "do_request", mocker_factory(response=server_response))
180181

181-
with pytest.raises(TelegramError, match="Invalid server response"):
182+
with pytest.raises(TelegramError, match="Invalid server response"), caplog.at_level(
183+
logging.ERROR
184+
):
182185
await httpx_request.post(None, None, None)
183186

187+
assert len(caplog.records) == 1
188+
record = caplog.records[0]
189+
assert record.name == "telegram.request.BaseRequest"
190+
assert record.getMessage().endswith(f'invalid JSON data: "{server_response.decode()}"')
191+
184192
async def test_chat_migrated(self, monkeypatch, httpx_request: HTTPXRequest):
185193
server_response = b'{"ok": "False", "parameters": {"migrate_to_chat_id": 123}}'
186194

0 commit comments

Comments
 (0)
0