Skip to content

Commit 5ab7775

Browse files
committed
JSON parsing error handling when server returns string
Internal JIRA Ticket: IMAGEKIT-132
1 parent 3856524 commit 5ab7775

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

imagekitio/file.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
snake_to_lower_camel,
1111
)
1212

13+
try:
14+
from simplejson.errors import JSONDecodeError
15+
except ImportError:
16+
from json import JSONDecodeError
1317

1418
class File(object):
1519
def __init__(self, request_obj):
@@ -47,7 +51,10 @@ def upload(self, file, file_name, options) -> Dict:
4751
)
4852

4953
if resp.status_code > 200:
50-
error = resp.json()
54+
try:
55+
error = resp.json()
56+
except JSONDecodeError:
57+
error = resp.text
5158
response = None
5259
else:
5360
error = None

tests/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
from imagekitio.client import ImageKit
88
from tests.dummy_data.file import AUTHENTICATION_ERR_MSG, SUCCESS_GENERIC_RESP
9+
try:
10+
from simplejson.errors import JSONDecodeError
11+
except ImportError:
12+
from json import JSONDecodeError
913

1014

1115
class ClientTestCase(unittest.TestCase):
@@ -41,6 +45,16 @@ def get_mocked_failed_resp(message=None, status=401):
4145
return mocked_resp
4246

4347

48+
def get_mocked_failed_resp_text():
49+
"""GET failed mocked response returned as text not json
50+
"""
51+
mocked_resp = Mock(spec=Response)
52+
mocked_resp.status_code = 502
53+
mocked_resp.text = 'Bad Gateway'
54+
mocked_resp.json.side_effect = JSONDecodeError("Expecting value: ", "Bad Gateway", 0)
55+
return mocked_resp
56+
57+
4458
def get_mocked_success_resp(message: dict = None, status: int = 200):
4559
"""GET success mocked response customize by parameter
4660
"""

tests/test_files_ops.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from tests.helpers import (
1616
ClientTestCase,
1717
get_mocked_failed_resp,
18+
get_mocked_failed_resp_text,
1819
get_mocked_success_resp,
1920
)
2021
from imagekitio.utils.formatter import request_formatter
@@ -173,6 +174,27 @@ def test_upload_file_fails_without_file_or_file_name(self) -> None:
173174
self.assertRaises(TypeError, self.client.upload_file, file_name=self.filename)
174175
self.assertRaises(TypeError, self.client.upload_file, file=self.image)
175176

177+
def test_upload_file_fails_without_json_response_from_server(self) -> None:
178+
"""Test upload raises error on non json response
179+
"""
180+
self.client.ik_request.request = MagicMock(
181+
return_value=get_mocked_failed_resp_text()
182+
)
183+
resp = self.client.upload(
184+
file=self.image,
185+
file_name="fileabc",
186+
options={
187+
"is_private_file": True,
188+
"tags": ["abc"],
189+
"response_fields": ["is_private_file", "tags"],
190+
"custom_coordinates": "10,10,100,100",
191+
"use_unique_file_name": True,
192+
"folder": "abc"
193+
}
194+
)
195+
self.assertIsNotNone(resp["error"])
196+
self.assertIsNone(resp["response"])
197+
176198

177199
class TestListFiles(ClientTestCase):
178200
"""

0 commit comments

Comments
 (0)