Skip to content

Commit

Permalink
Remove fictional error code in response (close #510)
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed May 8, 2024
1 parent 50f3e6f commit 0aed409
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 12 deletions.
8 changes: 4 additions & 4 deletions custom_components/yandex_smart_home/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
DeviceDescription,
DeviceList,
DeviceStates,
Error,
FailedActionResult,
Response,
ResponseCode,
Expand All @@ -45,16 +44,17 @@ async def async_handle_request(hass: HomeAssistant, data: RequestData, action: s

if handler is None:
_LOGGER.error(f"Unexpected action '{action}'")
return Response(request_id=data.request_id, payload=Error(error_code=ResponseCode.INTERNAL_ERROR))
return Response(request_id=data.request_id)

try:
return Response(request_id=data.request_id, payload=await handler(hass, data, payload))
except APIError as err:
_LOGGER.error(f"{err.message} ({err.code})")
return Response(request_id=data.request_id, payload=Error(error_code=ResponseCode(err.code)))
return Response(request_id=data.request_id)
except Exception:
# return always 200 due to blocking error on device page
_LOGGER.exception("Unexpected exception")
return Response(request_id=data.request_id, payload=Error(error_code=ResponseCode.INTERNAL_ERROR))
return Response(request_id=data.request_id)


@HANDLERS.register("/user/devices")
Expand Down
2 changes: 1 addition & 1 deletion custom_components/yandex_smart_home/schema/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ class Response(APIModel):
"""Base API response."""

request_id: str | None
payload: ResponsePayload | None
payload: ResponsePayload | None = None
5 changes: 1 addition & 4 deletions tests/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,7 @@ async def test_cloud_messages_invalid_format(hass_platform, config_entry_cloud,
await async_setup_entry(hass, config_entry_cloud, session=session)
mock_reconnect.assert_not_called()
await hass.config_entries.async_unload(config_entry_cloud.entry_id)
assert json.loads(session.ws.send_queue[0]) == {
"payload": {"error_code": "INTERNAL_ERROR"},
"request_id": "req",
}
assert json.loads(session.ws.send_queue[0]) == {"request_id": "req"}


async def test_cloud_req_user_devices(hass_platform, config_entry_cloud, aioclient_mock):
Expand Down
3 changes: 0 additions & 3 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,18 @@ async def none(*_, **__):
with patch("custom_components.yandex_smart_home.handlers.HANDLERS", r):
assert (await handlers.async_handle_request(hass, BASIC_REQUEST_DATA, "missing", "")).as_dict() == {
"request_id": REQ_ID,
"payload": {"error_code": "INTERNAL_ERROR"},
}
assert caplog.messages == ["Unexpected action 'missing'"]
caplog.clear()

assert (await handlers.async_handle_request(hass, BASIC_REQUEST_DATA, "error", "")).as_dict() == {
"request_id": REQ_ID,
"payload": {"error_code": "INVALID_ACTION"},
}
assert caplog.messages == ["foo (INVALID_ACTION)"]
caplog.clear()

assert (await handlers.async_handle_request(hass, BASIC_REQUEST_DATA, "exception", "")).as_dict() == {
"request_id": REQ_ID,
"payload": {"error_code": "INTERNAL_ERROR"},
}
assert caplog.records[-1].message == "Unexpected exception"
assert "boooo" in caplog.records[-1].exc_text
Expand Down

0 comments on commit 0aed409

Please sign in to comment.