From 8bbfee7801ce015180f05b97de9c3331b541c090 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Fri, 5 Jan 2024 15:44:31 +0100 Subject: [PATCH] Make exceptions in rest_command services translatable (#107252) --- .../components/rest_command/__init__.py | 37 +++++++++++-------- .../components/rest_command/strings.json | 11 ++++++ tests/components/rest_command/test_init.py | 24 +++++++++--- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/rest_command/__init__.py b/homeassistant/components/rest_command/__init__.py index a07ca03a258..7d566933b5f 100644 --- a/homeassistant/components/rest_command/__init__.py +++ b/homeassistant/components/rest_command/__init__.py @@ -169,28 +169,35 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: else: _content = await response.text() except (JSONDecodeError, AttributeError) as err: - _LOGGER.error("Response of `%s` has invalid JSON", request_url) - raise HomeAssistantError from err + raise HomeAssistantError( + f"Response of '{request_url}' could not be decoded as JSON", + translation_domain=DOMAIN, + translation_key="decoding_error", + translation_placeholders={"decoding_type": "json"}, + ) from err except UnicodeDecodeError as err: - _LOGGER.error( - "Response of `%s` could not be interpreted as text", - request_url, - ) - raise HomeAssistantError from err + raise HomeAssistantError( + f"Response of '{request_url}' could not be decoded as text", + translation_domain=DOMAIN, + translation_key="decoding_error", + translation_placeholders={"decoding_type": "text"}, + ) from err return {"content": _content, "status": response.status} except asyncio.TimeoutError as err: - _LOGGER.warning("Timeout call %s", request_url) - raise HomeAssistantError from err + raise HomeAssistantError( + f"Timeout when calling resource '{request_url}'", + translation_domain=DOMAIN, + translation_key="timeout", + ) from err except aiohttp.ClientError as err: - _LOGGER.error( - "Client error. Url: %s. Error: %s", - request_url, - err, - ) - raise HomeAssistantError from err + raise HomeAssistantError( + f"Client error occurred when calling resource '{request_url}'", + translation_domain=DOMAIN, + translation_key="client_error", + ) from err # register services hass.services.async_register( diff --git a/homeassistant/components/rest_command/strings.json b/homeassistant/components/rest_command/strings.json index 15f59ec8e29..8a48cddace3 100644 --- a/homeassistant/components/rest_command/strings.json +++ b/homeassistant/components/rest_command/strings.json @@ -4,5 +4,16 @@ "name": "[%key:common::action::reload%]", "description": "Reloads RESTful commands from the YAML-configuration." } + }, + "exceptions": { + "timeout": { + "message": "Timeout while waiting for response from the server" + }, + "client_error": { + "message": "An error occurred while requesting the resource" + }, + "decoding_error": { + "message": "The response from the server could not be decoded as {decoding_type}" + } } } diff --git a/tests/components/rest_command/test_init.py b/tests/components/rest_command/test_init.py index ce0359e0fdb..0e70f7bc52d 100644 --- a/tests/components/rest_command/test_init.py +++ b/tests/components/rest_command/test_init.py @@ -115,8 +115,11 @@ class TestRestCommandComponent: aioclient_mock.get(self.url, exc=asyncio.TimeoutError()) - self.hass.services.call(rc.DOMAIN, "get_test", {}) - self.hass.block_till_done() + with pytest.raises( + HomeAssistantError, + match=r"^Timeout when calling resource 'https://example.com/'$", + ): + self.hass.services.call(rc.DOMAIN, "get_test", {}, blocking=True) assert len(aioclient_mock.mock_calls) == 1 @@ -127,8 +130,11 @@ class TestRestCommandComponent: aioclient_mock.get(self.url, exc=aiohttp.ClientError()) - self.hass.services.call(rc.DOMAIN, "get_test", {}) - self.hass.block_till_done() + with pytest.raises( + HomeAssistantError, + match=r"^Client error occurred when calling resource 'https://example.com/'$", + ): + self.hass.services.call(rc.DOMAIN, "get_test", {}, blocking=True) assert len(aioclient_mock.mock_calls) == 1 @@ -412,7 +418,10 @@ class TestRestCommandComponent: assert not response # Throws error when requesting response - with pytest.raises(HomeAssistantError): + with pytest.raises( + HomeAssistantError, + match=r"^Response of 'https://example.com/' could not be decoded as JSON$", + ): response = self.hass.services.call( rc.DOMAIN, "get_test", {}, blocking=True, return_response=True ) @@ -439,7 +448,10 @@ class TestRestCommandComponent: assert not response # Throws Decode error when requesting response - with pytest.raises(HomeAssistantError): + with pytest.raises( + HomeAssistantError, + match=r"^Response of 'https://example.com/' could not be decoded as text$", + ): response = self.hass.services.call( rc.DOMAIN, "get_test", {}, blocking=True, return_response=True )