Skip to content

Commit 15cc961

Browse files
committed
fix: raise a ClientError exception for empty responses and JSON parsing errors
1 parent b365c15 commit 15cc961

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

kanboard.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(
6565
ignore_hostname_verification: bool = False,
6666
user_agent: str = "Kanboard Python API Client",
6767
loop: Optional[asyncio.AbstractEventLoop] = None,
68-
):
68+
) -> None:
6969
"""
7070
Initialize a new Kanboard API client instance.
7171
@@ -134,6 +134,8 @@ def _to_camel_case(snake_str: str) -> str:
134134

135135
@staticmethod
136136
def _parse_response(response: bytes):
137+
if not response:
138+
raise ClientError("Empty response from server")
137139
try:
138140
body = json.loads(response.decode(errors="ignore"))
139141

@@ -142,8 +144,8 @@ def _parse_response(response: bytes):
142144
raise ClientError(message)
143145

144146
return body.get("result")
145-
except ValueError:
146-
return None
147+
except ValueError as e:
148+
raise ClientError(f"Failed to parse JSON response: {e}")
147149

148150
def _do_request(self, headers: Dict[str, str], body: Dict):
149151
try:

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ authors = [
1313
classifiers = [
1414
"Intended Audience :: Developers",
1515
"Intended Audience :: Information Technology",
16-
"License :: OSI Approved :: MIT License",
1716
"Programming Language :: Python",
1817
"Programming Language :: Python :: 3 :: Only",
1918
]

tests/test_kanboard.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ def test_http_error(self):
6262
with self.assertRaises(kanboard.ClientError):
6363
self.client.remote_procedure()
6464

65+
def test_empty_response_raises_client_error(self):
66+
self.urlopen.return_value.read.return_value = b""
67+
with self.assertRaises(kanboard.ClientError) as cm:
68+
self.client.remote_procedure()
69+
self.assertIn("Empty response", str(cm.exception))
70+
71+
def test_json_parsing_failure(self):
72+
body = b"{invalid json}"
73+
self.urlopen.return_value.read.return_value = body
74+
with self.assertRaises(kanboard.ClientError) as cm:
75+
self.client.remote_procedure()
76+
self.assertIn("Failed to parse JSON response", str(cm.exception))
77+
6578
def test_application_error(self):
6679
body = b'{"jsonrpc": "2.0", "error": {"code": -32603, "message": "Internal error"}, "id": 123}'
6780
self.urlopen.return_value.read.return_value = body

0 commit comments

Comments
 (0)