Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle "resp invalid json" error (Closes: #205) #449

Merged
merged 2 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to have this codepath tested too (at some point, maybe there are also other recoverable cases?) :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you've an idea how to test this part?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way to go would be mocking the Message.parse() to return with that error code, and then checking that it gets called retry_count times. #437 may simplify the tests (or we might split the code even a bit more to make it easier to test). I think this can wait for now, feel free to merge.

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