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

feat: add default error handler #176

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def _delay(coroutine: Coroutine, delay_seconds: float):
await coroutine


async def _handle_error(ctx: Context, destination: str, msg: ErrorMessage):
async def _send_error_message(ctx: Context, destination: str, msg: ErrorMessage):
"""
Send an error message to the specified destination.

Expand Down Expand Up @@ -255,6 +255,11 @@ def __init__(
self._port, self._loop, self._queries, logger=self._logger
)

# define default error message handler
@self.on_message(ErrorMessage)
async def _handle_error_message(ctx: Context, sender: str, msg: ErrorMessage):
ctx.logger.exception(f"Received error message from {sender}: {msg.error}")

def _initialize_wallet_and_identity(self, seed, name):
"""
Initialize the wallet and identity for the agent.
Expand Down Expand Up @@ -823,7 +828,7 @@ async def _process_message_queue(self):
recovered = model_class.parse_raw(message)
except ValidationError as ex:
self._logger.warning(f"Unable to parse message: {ex}")
await _handle_error(
await _send_error_message(
context,
sender,
ErrorMessage(
Expand All @@ -840,7 +845,7 @@ async def _process_message_queue(self):
if not is_user_address(sender):
handler = self._signed_message_handlers.get(schema_digest)
elif schema_digest in self._signed_message_handlers:
await _handle_error(
await _send_error_message(
context,
sender,
ErrorMessage(
Expand Down
4 changes: 2 additions & 2 deletions python/tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _(_ctx, _sender, _msg):
unsigned_msg_handlers = self.agent._protocol._unsigned_message_handlers

self.assertEqual(len(unsigned_msg_handlers), 0)
self.assertEqual(len(signed_msg_handlers), 1)
self.assertEqual(len(signed_msg_handlers), 2)
self.assertTrue(isinstance(signed_msg_handlers[MESSAGE_DIGEST], Callable))

def test_agent_on_unsigned_message(self):
Expand All @@ -63,7 +63,7 @@ def _(_ctx, _sender, _msg):
unsigned_msg_handlers = self.agent._protocol._unsigned_message_handlers

self.assertEqual(len(unsigned_msg_handlers), 1)
self.assertEqual(len(signed_msg_handlers), 1)
self.assertEqual(len(signed_msg_handlers), 2)
self.assertTrue(isinstance(signed_msg_handlers[MESSAGE_DIGEST], Callable))
self.assertTrue(isinstance(unsigned_msg_handlers[QUERY_DIGEST], Callable))

Expand Down
Loading