Add ServiceValidationError and translation support (#102592)

* Add ServiceValidationError

* Add translation support

* Extend translation support to HomeAssistantError

* Add translation support for ServiceNotFound exc

* Frontend translation & translation_key from caller

* Improve fallback message

* Set websocket_api as default translation_domain

* Add MQTT ServiceValidationError exception

* Follow up comments

* Revert removing gueard on translation_key

* Revert test changes to fix CI test

* Follow up comments

* Fix CI test

* Follow up

* Improve language

* Follow up comment
This commit is contained in:
Jan Bouwhuis 2023-11-06 15:45:04 +01:00 committed by GitHub
parent 5cd61a0cf4
commit 54cf7010cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 206 additions and 18 deletions

View file

@ -65,12 +65,29 @@ def construct_result_message(iden: int, payload: str) -> str:
return f'{{"id":{iden},"type":"result","success":true,"result":{payload}}}'
def error_message(iden: int | None, code: str, message: str) -> dict[str, Any]:
def error_message(
iden: int | None,
code: str,
message: str,
translation_key: str | None = None,
translation_domain: str | None = None,
translation_placeholders: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Return an error result message."""
error_payload: dict[str, Any] = {
"code": code,
"message": message,
}
# In case `translation_key` is `None` we do not set it, nor the
# `translation`_placeholders` and `translation_domain`.
if translation_key is not None:
error_payload["translation_key"] = translation_key
error_payload["translation_placeholders"] = translation_placeholders
error_payload["translation_domain"] = translation_domain
return {
"id": iden,
**BASE_ERROR_MESSAGE,
"error": {"code": code, "message": message},
"error": error_payload,
}