Make exceptions in rest_command services translatable (#107252)
This commit is contained in:
parent
0d7627da22
commit
8bbfee7801
3 changed files with 51 additions and 21 deletions
|
@ -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(
|
||||
|
|
|
@ -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}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue