Skip to content

Commit 6ae69e9

Browse files
authored
feat: add warning in web requests if it fails to decode (#215)
1 parent af6f107 commit 6ae69e9

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

roborock/web_api.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import base64
44
import hashlib
55
import hmac
6+
import logging
67
import math
78
import secrets
89
import time
910

1011
import aiohttp
12+
from aiohttp import ContentTypeError
1113

1214
from roborock.containers import HomeData, HomeDataRoom, ProductResponse, RRiot, UserData
1315
from roborock.exceptions import (
@@ -23,6 +25,8 @@
2325
RoborockUrlException,
2426
)
2527

28+
_LOGGER = logging.getLogger(__name__)
29+
2630

2731
class RoborockApiClient:
2832
def __init__(self, username: str, base_url=None) -> None:
@@ -294,11 +298,23 @@ async def request(self, method: str, url: str, params=None, data=None, headers=N
294298
_url = "/".join(s.strip("/") for s in [self.base_url, url])
295299
_headers = {**self.base_headers, **(headers or {})}
296300
async with aiohttp.ClientSession() as session:
297-
async with session.request(
298-
method,
299-
_url,
300-
params=params,
301-
data=data,
302-
headers=_headers,
303-
) as resp:
304-
return await resp.json()
301+
try:
302+
async with session.request(
303+
method,
304+
_url,
305+
params=params,
306+
data=data,
307+
headers=_headers,
308+
) as resp:
309+
return await resp.json()
310+
except ContentTypeError as err:
311+
"""If we get an error, lets log everything for debugging."""
312+
try:
313+
resp_json = await resp.json(content_type=None)
314+
_LOGGER.info("Resp: %s", resp_json)
315+
except ContentTypeError as err_2:
316+
_LOGGER.info(err_2)
317+
resp_raw = await resp.read()
318+
_LOGGER.info("Resp raw: %s", resp_raw)
319+
# Still raise the err so that it's clear it failed.
320+
raise err

0 commit comments

Comments
 (0)