Send localization info on websocket_api script errors (#104638)

* Send localization info on script errors

* Use connection exception hander

* Keep HomeAssistantError is unknown_error

* Move specific exception handling
This commit is contained in:
Jan Bouwhuis 2023-11-29 10:47:23 +01:00 committed by GitHub
parent 7dbaf40f48
commit efd330f182
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 3 deletions

View file

@ -255,7 +255,10 @@ class ActiveConnection:
log_handler = self.logger.error
code = const.ERR_UNKNOWN_ERROR
err_message = None
err_message: str | None = None
translation_domain: str | None = None
translation_key: str | None = None
translation_placeholders: dict[str, Any] | None = None
if isinstance(err, Unauthorized):
code = const.ERR_UNAUTHORIZED
@ -268,6 +271,10 @@ class ActiveConnection:
err_message = "Timeout"
elif isinstance(err, HomeAssistantError):
err_message = str(err)
code = const.ERR_UNKNOWN_ERROR
translation_domain = err.translation_domain
translation_key = err.translation_key
translation_placeholders = err.translation_placeholders
# This if-check matches all other errors but also matches errors which
# result in an empty message. In that case we will also log the stack
@ -276,7 +283,16 @@ class ActiveConnection:
err_message = "Unknown error"
log_handler = self.logger.exception
self.send_message(messages.error_message(msg["id"], code, err_message))
self.send_message(
messages.error_message(
msg["id"],
code,
err_message,
translation_domain=translation_domain,
translation_key=translation_key,
translation_placeholders=translation_placeholders,
)
)
if code:
err_message += f" ({code})"