From 821b941774f2518b207e779a4b1847cd70484a73 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Wed, 6 Mar 2024 21:10:10 +0100 Subject: [PATCH 1/3] Enhance logging failed client commands a bit --- matter_server/server/client_handler.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/matter_server/server/client_handler.py b/matter_server/server/client_handler.py index 0c695303..4cfa9fbc 100644 --- a/matter_server/server/client_handler.py +++ b/matter_server/server/client_handler.py @@ -192,26 +192,27 @@ async def _run_handler( if asyncio.iscoroutine(result): result = await result self._send_message(SuccessResultMessage(msg.message_id, result)) - except ChipStackError as err: + except (ChipStackError, MatterError) as err: + error_code = getattr(err, "error_code", MatterError.error_code) + message_str = msg.command + if msg.args and (node_id := msg.args.get("node_id")): + message_str += f" (node {node_id})" self._logger.error( - "SDK Error during handling message: %s: %s", + "Error while handling command: %s: %s", msg.command, - str(err), + str(err) or err.__class__.__name__, # only print the full stacktrace if debug logging is enabled exc_info=err if self._logger.isEnabledFor(logging.DEBUG) else None, ) self._send_message( ErrorResultMessage(msg.message_id, SDKStackError.error_code, str(err)) ) - except Exception as err: # pylint: disable=broad-except # noqa: BLE001 - self._logger.error( - "SDK Error during handling message: %s: %s", + except Exception as err: # pylint: disable=broad-except + self._logger.exception( + "Unhandled exception while handling command: %s", msg.command, - str(err), - # only print the full stacktrace if debug logging is enabled - exc_info=err if self._logger.isEnabledFor(logging.DEBUG) else None, ) - error_code = getattr(err, "error_code", MatterError.error_code) + error_code = getattr(err, "error_code", 0) self._send_message(ErrorResultMessage(msg.message_id, error_code, str(err))) async def _writer(self) -> None: From b248f286a05f0f428f5b0c1b5983101cc9c9dc24 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Wed, 6 Mar 2024 21:21:21 +0100 Subject: [PATCH 2/3] rephrase a bit --- matter_server/server/client_handler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matter_server/server/client_handler.py b/matter_server/server/client_handler.py index 4cfa9fbc..f291ef00 100644 --- a/matter_server/server/client_handler.py +++ b/matter_server/server/client_handler.py @@ -198,8 +198,8 @@ async def _run_handler( if msg.args and (node_id := msg.args.get("node_id")): message_str += f" (node {node_id})" self._logger.error( - "Error while handling command: %s: %s", - msg.command, + "Error while handling: %s: %s", + message_str, str(err) or err.__class__.__name__, # only print the full stacktrace if debug logging is enabled exc_info=err if self._logger.isEnabledFor(logging.DEBUG) else None, @@ -209,7 +209,7 @@ async def _run_handler( ) except Exception as err: # pylint: disable=broad-except self._logger.exception( - "Unhandled exception while handling command: %s", + "Unhandled exception while handling: %s", msg.command, ) error_code = getattr(err, "error_code", 0) From d5a32f0e34051e528f7f56020ebb5be1942170a3 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Wed, 6 Mar 2024 21:25:03 +0100 Subject: [PATCH 3/3] just re-raise unhandled exceptions --- matter_server/server/client_handler.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/matter_server/server/client_handler.py b/matter_server/server/client_handler.py index f291ef00..771b0b27 100644 --- a/matter_server/server/client_handler.py +++ b/matter_server/server/client_handler.py @@ -15,7 +15,7 @@ from matter_server.common.helpers.json import json_dumps, json_loads from matter_server.common.models import EventType -from ..common.errors import InvalidArguments, InvalidCommand, MatterError, SDKStackError +from ..common.errors import InvalidArguments, InvalidCommand, MatterError from ..common.helpers.api import parse_arguments from ..common.helpers.util import dataclass_from_dict from ..common.models import ( @@ -204,16 +204,10 @@ async def _run_handler( # only print the full stacktrace if debug logging is enabled exc_info=err if self._logger.isEnabledFor(logging.DEBUG) else None, ) - self._send_message( - ErrorResultMessage(msg.message_id, SDKStackError.error_code, str(err)) - ) - except Exception as err: # pylint: disable=broad-except - self._logger.exception( - "Unhandled exception while handling: %s", - msg.command, - ) - error_code = getattr(err, "error_code", 0) self._send_message(ErrorResultMessage(msg.message_id, error_code, str(err))) + except Exception as err: + self._send_message(ErrorResultMessage(msg.message_id, 0, str(err))) + raise err async def _writer(self) -> None: """Write outgoing messages."""