Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better logging of client errors #620

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions matter_server/server/client_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -192,27 +192,22 @@ async def _run_handler(
if asyncio.iscoroutine(result):
result = await result
self._send_message(SuccessResultMessage(msg.message_id, result))
except ChipStackError as err:
self._logger.error(
"SDK Error during handling message: %s: %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,
)
self._send_message(
ErrorResultMessage(msg.message_id, SDKStackError.error_code, str(err))
)
except Exception as err: # pylint: disable=broad-except # noqa: BLE001
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",
msg.command,
str(err),
"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,
)
error_code = getattr(err, "error_code", MatterError.error_code)
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."""
Expand Down