Skip to content

Commit 805d084

Browse files
committed
[http] improve errors handling
1 parent 3304412 commit 805d084

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

android_sms_gateway/ahttp.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import abc
2+
from contextlib import suppress
23
import typing as t
34

45
from .errors import (
@@ -81,14 +82,13 @@ async def _process_response(self, response: aiohttp.ClientResponse) -> dict:
8182
return {}
8283

8384
return await response.json()
85+
except aiohttp.ContentTypeError:
86+
return {}
8487
except aiohttp.ClientResponseError as e:
8588
# Extract error message from response if available
8689
error_data = {}
87-
try:
90+
with suppress(ValueError, aiohttp.ContentTypeError):
8891
error_data = await response.json()
89-
except ValueError:
90-
# Response is not JSON
91-
pass
9292

9393
# Use the error mapping to create appropriate exception
9494
error_message = str(e) or "HTTP request failed"
@@ -188,15 +188,16 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
188188
async def _process_response(self, response: httpx.Response) -> dict:
189189
try:
190190
response.raise_for_status()
191-
if response.status_code == 204:
191+
if response.status_code == 204 or not response.content:
192192
return {}
193193

194194
return response.json()
195195
except httpx.HTTPStatusError as e:
196196
# Extract error message from response if available
197197
error_data = {}
198198
try:
199-
error_data = response.json()
199+
if response.content:
200+
error_data = response.json()
200201
except ValueError:
201202
# Response is not JSON
202203
pass

android_sms_gateway/http.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,16 @@ def patch(
133133
def _process_response(self, response: requests.Response) -> dict:
134134
try:
135135
response.raise_for_status()
136-
if response.status_code == 204:
136+
if response.status_code == 204 or not response.content:
137137
return {}
138138

139139
return response.json()
140140
except requests.exceptions.HTTPError as e:
141141
# Extract error message from response if available
142142
error_data = {}
143143
try:
144-
error_data = response.json()
144+
if response.content:
145+
error_data = response.json()
145146
except ValueError:
146147
# Response is not JSON
147148
pass
@@ -181,15 +182,16 @@ def __exit__(self, exc_type, exc_val, exc_tb):
181182
def _process_response(self, response: httpx.Response) -> dict:
182183
try:
183184
response.raise_for_status()
184-
if response.status_code == 204:
185+
if response.status_code == 204 or not response.content:
185186
return {}
186187

187188
return response.json()
188189
except httpx.HTTPStatusError as e:
189190
# Extract error message from response if available
190191
error_data = {}
191192
try:
192-
error_data = response.json()
193+
if response.content:
194+
error_data = response.json()
193195
except ValueError:
194196
# Response is not JSON
195197
pass

0 commit comments

Comments
 (0)