Skip to content

Commit d9fe80a

Browse files
committed
Improve JSON decoding error logging
1 parent 9947869 commit d9fe80a

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

reportportal_client/core/rp_responses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def json(self) -> Any:
8989
try:
9090
self.__json = self._resp.json()
9191
except (JSONDecodeError, TypeError) as exc:
92-
logger.warning(_get_json_decode_error_message(self._resp), exc_info=exc)
92+
logger.error(_get_json_decode_error_message(self._resp), exc_info=exc)
9393
self.__json = None
9494
return self.__json
9595

@@ -157,7 +157,7 @@ async def json(self) -> Any:
157157
try:
158158
self.__json = await self._resp.json()
159159
except (JSONDecodeError, TypeError) as exc:
160-
logger.warning(_get_json_decode_error_message(self._resp), exc_info=exc)
160+
logger.error(_get_json_decode_error_message(self._resp), exc_info=exc)
161161
self.__json = None
162162
return self.__json
163163

tests/core/test_rp_responses.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (c) 2023 EPAM Systems
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# https://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License
13+
14+
import json
15+
import sys
16+
import pytest
17+
18+
from unittest import mock
19+
20+
from reportportal_client.core.rp_responses import RPResponse, AsyncRPResponse
21+
22+
23+
def json_error():
24+
raise json.JSONDecodeError('Expecting value: line 1 column 1 (char 0)', '<html />', 0)
25+
26+
27+
@mock.patch('reportportal_client.core.rp_responses.logging.Logger.error')
28+
def test_json_decode_error(error_log):
29+
response = mock.Mock()
30+
response.ok = False
31+
del response.status
32+
response.status_code = 404
33+
response.json.side_effect = json_error
34+
35+
rp_response = RPResponse(response)
36+
assert rp_response.json is None
37+
error_log.assert_called_once()
38+
assert error_log.call_args_list[0][0][0] == ('Unable to decode JSON response, got failed response with code "404" '
39+
'please check your endpoint configuration or API key')
40+
41+
42+
@pytest.mark.skipif(sys.version_info < (3, 8),
43+
reason='the test requires AsyncMock which was introduced in Python 3.8')
44+
@mock.patch('reportportal_client.core.rp_responses.logging.Logger.error')
45+
@pytest.mark.asyncio
46+
async def test_json_decode_error_async(error_log):
47+
response = mock.AsyncMock()
48+
response.ok = False
49+
response.status = 403
50+
response.json.side_effect = json_error
51+
52+
rp_response = AsyncRPResponse(response)
53+
assert await rp_response.json is None
54+
error_log.assert_called_once()
55+
assert error_log.call_args_list[0][0][0] == ('Unable to decode JSON response, got failed response with code "403" '
56+
'please check your endpoint configuration or API key')

0 commit comments

Comments
 (0)