Skip to content

Commit

Permalink
Handle "resp invalid json" error (Closes: #205) (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jan 10, 2019
1 parent 6e1f803 commit f08b615
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
18 changes: 14 additions & 4 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .click_common import (
DeviceGroupMeta, command, format_output, LiteralParamType
)
from .exceptions import DeviceException, DeviceError
from .exceptions import DeviceException, DeviceError, RecoverableError
from .protocol import Message

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -269,7 +269,10 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
m.data.value["id"],
m.data.value)
if "error" in m.data.value:
raise DeviceError(m.data.value["error"])
error = m.data.value["error"]
if "code" in error and error["code"] == -30001:
raise RecoverableError(error)
raise DeviceError(error)

try:
return m.data.value["result"]
Expand All @@ -281,15 +284,22 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
"Please check your token!") from ex
except OSError as ex:
if retry_count > 0:
_LOGGER.debug("Retrying with incremented id, "
"retries left: %s", retry_count)
_LOGGER.debug("Retrying with incremented id, retries left: %s", retry_count)
self.__id += 100
self._discovered = False
return self.send(command, parameters, retry_count - 1)

_LOGGER.error("Got error when receiving: %s", ex)
raise DeviceException("No response from the device") from ex

except RecoverableError as ex:
if retry_count > 0:
_LOGGER.debug("Retrying to send failed command, retries left: %s", retry_count)
return self.send(command, parameters, retry_count - 1)

_LOGGER.error("Got error when receiving: %s", ex)
raise DeviceException("Unable to recover failed command") from ex

@command(
click.argument('command', type=str, required=True),
click.argument('parameters', type=LiteralParamType(), required=False),
Expand Down
5 changes: 5 additions & 0 deletions miio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ class DeviceException(Exception):
class DeviceError(DeviceException):
"""Exception communicating an error delivered by the target device."""
pass


class RecoverableError(DeviceError):
"""Exception communicating an recoverable error delivered by the target device."""
pass

0 comments on commit f08b615

Please sign in to comment.