Skip to content

Commit

Permalink
Fix logging undecodable responses (#1626)
Browse files Browse the repository at this point in the history
The logging statement was using incorrect variable for undecodables,
this fixes that and will change the log level to error to make it also
more visible.
  • Loading branch information
rytilahti authored Dec 14, 2022
1 parent e198638 commit cdca98f
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions miio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import hashlib
import json
import logging
from typing import Any, Dict, Tuple
from typing import Any, Dict, Tuple, Union

from construct import (
Adapter,
Expand Down Expand Up @@ -159,18 +159,13 @@ def _encode(self, obj, context, path):
json.dumps(obj).encode("utf-8") + b"\x00", context["_"]["token"]
)

def _decode(self, obj, context, path):
"""Decrypts the given payload with the token stored in the context.
:return str: JSON object
"""
def _decode(self, obj, context, path) -> Union[Dict, bytes]:
"""Decrypts the payload using the token stored in the context."""
try:
# pp(context)
decrypted = Utils.decrypt(obj, context["_"]["token"])
decrypted = decrypted.rstrip(b"\x00")
except Exception:
if obj:
_LOGGER.debug("Unable to decrypt, returning raw bytes: %s", obj)
_LOGGER.debug("Unable to decrypt, returning raw bytes: %s", obj)
return obj

# list of adaption functions for malformed json payload (quirks)
Expand Down Expand Up @@ -201,12 +196,12 @@ def _decode(self, obj, context, path):
# log the error when decrypted bytes couldn't be loaded
# after trying all quirk adaptions
if i == len(decrypted_quirks) - 1:
_LOGGER.debug("Unable to parse json '%s': %s", decoded, ex)
_LOGGER.error("Unable to parse json '%s': %s", decrypted, ex)
raise PayloadDecodeException(
"Unable to parse message payload"
) from ex

return None
raise Exception("this should never happen")


Message = Struct(
Expand Down

0 comments on commit cdca98f

Please sign in to comment.