Skip to content

Commit

Permalink
feat: add default error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jrriehl committed Oct 4, 2023
1 parent 992a462 commit a843ca2
Showing 1 changed file with 8 additions and 3 deletions.
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, allow_unverified=True)
async def _handle_error_message(ctx: Context, sender: str, msg: ErrorMessage):
ctx.logger.warning(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

0 comments on commit a843ca2

Please sign in to comment.