diff --git a/homeassistant/components/websocket_api/connection.py b/homeassistant/components/websocket_api/connection.py index 075aed86453..9b53f358b85 100644 --- a/homeassistant/components/websocket_api/connection.py +++ b/homeassistant/components/websocket_api/connection.py @@ -121,6 +121,9 @@ class ActiveConnection: """Handle an exception while processing a handler.""" log_handler = self.logger.error + code = const.ERR_UNKNOWN_ERROR + err_message = None + if isinstance(err, Unauthorized): code = const.ERR_UNAUTHORIZED err_message = "Unauthorized" @@ -131,13 +134,15 @@ class ActiveConnection: code = const.ERR_TIMEOUT err_message = "Timeout" elif isinstance(err, HomeAssistantError): - code = const.ERR_UNKNOWN_ERROR err_message = str(err) - else: - code = const.ERR_UNKNOWN_ERROR + + # 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 + # trace so it can be fixed. + if not err_message: err_message = "Unknown error" log_handler = self.logger.exception - log_handler("Error handling message: %s", err_message) + log_handler("Error handling message: %s (%s)", err_message, code) self.send_message(messages.error_message(msg["id"], code, err_message)) diff --git a/tests/components/websocket_api/test_connection.py b/tests/components/websocket_api/test_connection.py index 0a9ae710bc5..3a54d5912e0 100644 --- a/tests/components/websocket_api/test_connection.py +++ b/tests/components/websocket_api/test_connection.py @@ -54,6 +54,11 @@ async def test_exception_handling(): "Failed to do X", ), (ValueError("Really bad"), websocket_api.ERR_UNKNOWN_ERROR, "Unknown error"), + ( + exceptions.HomeAssistantError(), + websocket_api.ERR_UNKNOWN_ERROR, + "Unknown error", + ), ): send_messages.clear() conn.async_handle_exception({"id": 5}, exc)