Skip to content

Commit

Permalink
fix: catch orjson Decode errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Aug 27, 2023
1 parent 3c5b5b5 commit 5be987e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
74 changes: 46 additions & 28 deletions teslajsonpy/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ async def __open(
)
except httpx.HTTPStatusError as exception_:
raise TeslaException(exception_.request.status_code) from exception_
except orjson.JSONDecodeError as exception_:
raise TeslaException("Error decoding response into json") from exception_
return data

async def websocket_connect(self, vin: int, vehicle_id: int, **kwargs):
Expand All @@ -247,31 +249,35 @@ async def _process_messages() -> None:
async for msg in self.websocket:
_LOGGER.debug("msg: %s", msg)
if msg.type == aiohttp.WSMsgType.BINARY:
msg_json = orjson.loads(msg.data) # pylint: disable=no-member
if msg_json["msg_type"] == "control:hello":
_LOGGER.debug(
"%s:Succesfully connected to websocket %s",
vin[-5:],
self.websocket_url,
)
if msg_json["msg_type"] == "data:update":
last_message_time = time.time()
if (
msg_json["msg_type"] == "data:error"
and msg_json["value"] == "Can't validate token. "
):
raise TeslaException(
"Can't validate token for websocket connection."
)
if (
msg_json["msg_type"] == "data:error"
and msg_json["value"] == "disconnected"
):
if kwargs.get("on_disconnect"):
kwargs.get("on_disconnect")(msg_json)
disconnected = True
if kwargs.get("on_message"):
kwargs.get("on_message")(msg_json)
try:
msg_json = orjson.loads(msg.data) # pylint: disable=no-member
if msg_json["msg_type"] == "control:hello":
_LOGGER.debug(
"%s:Succesfully connected to websocket %s",
vin[-5:],
self.websocket_url,
)
if msg_json["msg_type"] == "data:update":
last_message_time = time.time()
if (
msg_json["msg_type"] == "data:error"
and msg_json["value"] == "Can't validate token. "
):
raise TeslaException(
"Can't validate token for websocket connection."
)
if (
msg_json["msg_type"] == "data:error"
and msg_json["value"] == "disconnected"
):
if kwargs.get("on_disconnect"):
kwargs.get("on_disconnect")(msg_json)
disconnected = True
if kwargs.get("on_message"):
kwargs.get("on_message")(msg_json)
except orjson.JSONDecodeError:
_LOGGER.debug("Received bad websocket message: %s", msg.data)
break
elif msg.type == aiohttp.WSMsgType.ERROR:
_LOGGER.debug("WSMsgType error")
break
Expand Down Expand Up @@ -538,7 +544,11 @@ async def get_sso_auth_token(self, code):
str(self.auth_domain.with_path("/oauth2/v3/token")),
data=oauth,
)
return orjson.loads(auth.text) # pylint: disable=no-member
try:
return orjson.loads(auth.text) # pylint: disable=no-member
except orjson.JSONDecodeError:
_LOGGER.debug("Received bad auth response: %s", auth.text)
return None

async def refresh_access_token(self, refresh_token):
"""Refresh access token from sso."""
Expand All @@ -557,7 +567,11 @@ async def refresh_access_token(self, refresh_token):
str(self.auth_domain.with_path("/oauth2/v3/token")),
data=oauth,
)
return orjson.loads(auth.text) # pylint: disable=no-member
try:
return orjson.loads(auth.text) # pylint: disable=no-member
except orjson.JSONDecodeError:
_LOGGER.debug("Received bad auth response: %s", auth.text)
return None

async def get_bearer_token(self, access_token):
"""Get bearer token. This is used by the owners API."""
Expand All @@ -577,7 +591,11 @@ async def get_bearer_token(self, access_token):
auth = await self.websession.post(
"https://owner-api.teslamotors.com/oauth/token", headers=head, data=oauth
)
return orjson.loads(auth.text) # pylint: disable=no-member
try:
return orjson.loads(auth.text) # pylint: disable=no-member
except orjson.JSONDecodeError:
_LOGGER.debug("Received bad auth response: %s", auth.text)
return None


def get_inputs(soup: BeautifulSoup, searchfield=None) -> Dict[str, str]:
Expand Down
2 changes: 1 addition & 1 deletion teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ async def api(
data.decode()
)
_LOGGER.debug("%d endpoints loaded", len(self.endpoints))
except (IOError, ValueError):
except (IOError, ValueError, orjson.JSONDecodeError):
_LOGGER.error("No endpoints loaded")
# Lookup endpoint name
try:
Expand Down

0 comments on commit 5be987e

Please sign in to comment.