-
Currently, I have a few options to alert the user to errors in the code. So the question is this, how can I send a message to the user with an error for those low level functions without breaking the execution flow? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I solved it by using the frame stack: def _get_frame_context(self, logger=None):
"""Inspect the stack to find the context object from invoke"""
frame_stack = inspect_stack()
for frame_info in frame_stack:
if frame_info.function.lower() == "invoke":
context = frame_info.frame.f_locals["ctx"]
break
else:
logger = self.logger if not logger else logger
raise CommandError("Could not find context object", logger)
del frame_stack
return context
async def send_error(self, message: str, logger: logging.Logger, context=None):
"""
:param message: The error message
:param logger: The logger from where the error occured
Send a message to the invocation channel, without disrupting execution
if can find context
"""
if context is None:
try:
context = await self._get_frame_context()
except CommandError:
raise CommandError(f"Could not find context object: {message}", logger)
await context.send(message)
logger.warning(message)
``` |
Beta Was this translation helpful? Give feedback.
I solved it by using the frame stack: