diff --git a/homeassistant/components/hassio/const.py b/homeassistant/components/hassio/const.py index b495745e87d..b31c0f1cf15 100644 --- a/homeassistant/components/hassio/const.py +++ b/homeassistant/components/hassio/const.py @@ -19,6 +19,7 @@ ATTR_HOMEASSISTANT = "homeassistant" ATTR_HOMEASSISTANT_EXCLUDE_DATABASE = "homeassistant_exclude_database" ATTR_INPUT = "input" ATTR_ISSUES = "issues" +ATTR_MESSAGE = "message" ATTR_METHOD = "method" ATTR_PANELS = "panels" ATTR_PASSWORD = "password" diff --git a/homeassistant/components/hassio/handler.py b/homeassistant/components/hassio/handler.py index 9b8e6367647..65c0824dbd2 100644 --- a/homeassistant/components/hassio/handler.py +++ b/homeassistant/components/hassio/handler.py @@ -21,7 +21,7 @@ from homeassistant.const import SERVER_PORT from homeassistant.core import HomeAssistant from homeassistant.loader import bind_hass -from .const import ATTR_DISCOVERY, DOMAIN, X_HASS_SOURCE +from .const import ATTR_DISCOVERY, ATTR_MESSAGE, ATTR_RESULT, DOMAIN, X_HASS_SOURCE _P = ParamSpec("_P") @@ -576,7 +576,7 @@ class HassIO: raise HassioAPIError() try: - request = await self.websession.request( + response = await self.websession.request( method, joined_url, json=payload, @@ -589,14 +589,23 @@ class HassIO: timeout=aiohttp.ClientTimeout(total=timeout), ) - if request.status != HTTPStatus.OK: - _LOGGER.error("%s return code %d", command, request.status) + if response.status != HTTPStatus.OK: + error = await response.json(encoding="utf-8") + if error.get(ATTR_RESULT) == "error": + raise HassioAPIError(error.get(ATTR_MESSAGE)) + + _LOGGER.error( + "Request to %s method %s returned with code %d", + command, + method, + response.status, + ) raise HassioAPIError() if return_text: - return await request.text(encoding="utf-8") + return await response.text(encoding="utf-8") - return await request.json(encoding="utf-8") + return await response.json(encoding="utf-8") except TimeoutError: _LOGGER.error("Timeout on %s request", command) diff --git a/homeassistant/components/hassio/websocket_api.py b/homeassistant/components/hassio/websocket_api.py index cf59f8de7f7..13c258dd68c 100644 --- a/homeassistant/components/hassio/websocket_api.py +++ b/homeassistant/components/hassio/websocket_api.py @@ -21,7 +21,6 @@ from .const import ( ATTR_DATA, ATTR_ENDPOINT, ATTR_METHOD, - ATTR_RESULT, ATTR_SESSION_DATA_USER_ID, ATTR_TIMEOUT, ATTR_WS_EVENT, @@ -131,9 +130,6 @@ async def websocket_supervisor_api( payload=payload, source="core.websocket_api", ) - - if result.get(ATTR_RESULT) == "error": - raise HassioAPIError(result.get("message")) except HassioAPIError as err: _LOGGER.error("Failed to to call %s - %s", msg[ATTR_ENDPOINT], err) connection.send_error( diff --git a/tests/components/hassio/test_websocket_api.py b/tests/components/hassio/test_websocket_api.py index b2f9e06cb43..ee17ad62e74 100644 --- a/tests/components/hassio/test_websocket_api.py +++ b/tests/components/hassio/test_websocket_api.py @@ -171,6 +171,7 @@ async def test_websocket_supervisor_api_error( aioclient_mock.get( "http://127.0.0.1/ping", json={"result": "error", "message": "example error"}, + status=400, ) await websocket_client.send_json( @@ -183,9 +184,39 @@ async def test_websocket_supervisor_api_error( ) msg = await websocket_client.receive_json() + assert msg["error"]["code"] == "unknown_error" assert msg["error"]["message"] == "example error" +async def test_websocket_supervisor_api_error_without_msg( + hassio_env, + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + aioclient_mock: AiohttpClientMocker, +) -> None: + """Test Supervisor websocket api error.""" + assert await async_setup_component(hass, "hassio", {}) + websocket_client = await hass_ws_client(hass) + aioclient_mock.get( + "http://127.0.0.1/ping", + json={}, + status=400, + ) + + await websocket_client.send_json( + { + WS_ID: 1, + WS_TYPE: WS_TYPE_API, + ATTR_ENDPOINT: "/ping", + ATTR_METHOD: "get", + } + ) + + msg = await websocket_client.receive_json() + assert msg["error"]["code"] == "unknown_error" + assert msg["error"]["message"] == "" + + async def test_websocket_non_admin_user( hassio_env, hass: HomeAssistant,