Log stack trace if exception without message (#67587)

This commit is contained in:
Paulus Schoutsen 2022-03-04 00:48:07 -08:00 committed by GitHub
parent 57ffc65af2
commit fbc39d1206
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View file

@ -121,6 +121,9 @@ class ActiveConnection:
"""Handle an exception while processing a handler.""" """Handle an exception while processing a handler."""
log_handler = self.logger.error log_handler = self.logger.error
code = const.ERR_UNKNOWN_ERROR
err_message = None
if isinstance(err, Unauthorized): if isinstance(err, Unauthorized):
code = const.ERR_UNAUTHORIZED code = const.ERR_UNAUTHORIZED
err_message = "Unauthorized" err_message = "Unauthorized"
@ -131,13 +134,15 @@ class ActiveConnection:
code = const.ERR_TIMEOUT code = const.ERR_TIMEOUT
err_message = "Timeout" err_message = "Timeout"
elif isinstance(err, HomeAssistantError): elif isinstance(err, HomeAssistantError):
code = const.ERR_UNKNOWN_ERROR
err_message = str(err) 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" err_message = "Unknown error"
log_handler = self.logger.exception 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)) self.send_message(messages.error_message(msg["id"], code, err_message))

View file

@ -54,6 +54,11 @@ async def test_exception_handling():
"Failed to do X", "Failed to do X",
), ),
(ValueError("Really bad"), websocket_api.ERR_UNKNOWN_ERROR, "Unknown error"), (ValueError("Really bad"), websocket_api.ERR_UNKNOWN_ERROR, "Unknown error"),
(
exceptions.HomeAssistantError(),
websocket_api.ERR_UNKNOWN_ERROR,
"Unknown error",
),
): ):
send_messages.clear() send_messages.clear()
conn.async_handle_exception({"id": 5}, exc) conn.async_handle_exception({"id": 5}, exc)