Make exceptions in rest_command services translatable (#107252)

This commit is contained in:
Jan-Philipp Benecke 2024-01-05 15:44:31 +01:00 committed by GitHub
parent 0d7627da22
commit 8bbfee7801
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 21 deletions

View file

@ -169,28 +169,35 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
else: else:
_content = await response.text() _content = await response.text()
except (JSONDecodeError, AttributeError) as err: except (JSONDecodeError, AttributeError) as err:
_LOGGER.error("Response of `%s` has invalid JSON", request_url) raise HomeAssistantError(
raise HomeAssistantError from err 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: except UnicodeDecodeError as err:
_LOGGER.error( raise HomeAssistantError(
"Response of `%s` could not be interpreted as text", f"Response of '{request_url}' could not be decoded as text",
request_url, translation_domain=DOMAIN,
) translation_key="decoding_error",
raise HomeAssistantError from err translation_placeholders={"decoding_type": "text"},
) from err
return {"content": _content, "status": response.status} return {"content": _content, "status": response.status}
except asyncio.TimeoutError as err: except asyncio.TimeoutError as err:
_LOGGER.warning("Timeout call %s", request_url) raise HomeAssistantError(
raise HomeAssistantError from err f"Timeout when calling resource '{request_url}'",
translation_domain=DOMAIN,
translation_key="timeout",
) from err
except aiohttp.ClientError as err: except aiohttp.ClientError as err:
_LOGGER.error( raise HomeAssistantError(
"Client error. Url: %s. Error: %s", f"Client error occurred when calling resource '{request_url}'",
request_url, translation_domain=DOMAIN,
err, translation_key="client_error",
) ) from err
raise HomeAssistantError from err
# register services # register services
hass.services.async_register( hass.services.async_register(

View file

@ -4,5 +4,16 @@
"name": "[%key:common::action::reload%]", "name": "[%key:common::action::reload%]",
"description": "Reloads RESTful commands from the YAML-configuration." "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}"
}
} }
} }

View file

@ -115,8 +115,11 @@ class TestRestCommandComponent:
aioclient_mock.get(self.url, exc=asyncio.TimeoutError()) aioclient_mock.get(self.url, exc=asyncio.TimeoutError())
self.hass.services.call(rc.DOMAIN, "get_test", {}) with pytest.raises(
self.hass.block_till_done() 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 assert len(aioclient_mock.mock_calls) == 1
@ -127,8 +130,11 @@ class TestRestCommandComponent:
aioclient_mock.get(self.url, exc=aiohttp.ClientError()) aioclient_mock.get(self.url, exc=aiohttp.ClientError())
self.hass.services.call(rc.DOMAIN, "get_test", {}) with pytest.raises(
self.hass.block_till_done() 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 assert len(aioclient_mock.mock_calls) == 1
@ -412,7 +418,10 @@ class TestRestCommandComponent:
assert not response assert not response
# Throws error when requesting 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( response = self.hass.services.call(
rc.DOMAIN, "get_test", {}, blocking=True, return_response=True rc.DOMAIN, "get_test", {}, blocking=True, return_response=True
) )
@ -439,7 +448,10 @@ class TestRestCommandComponent:
assert not response assert not response
# Throws Decode error when requesting 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( response = self.hass.services.call(
rc.DOMAIN, "get_test", {}, blocking=True, return_response=True rc.DOMAIN, "get_test", {}, blocking=True, return_response=True
) )