handle exception in command creation

This commit is contained in:
HgO 2022-07-16 23:20:25 +02:00
parent 76b8ead9d7
commit fba263afe8
2 changed files with 55 additions and 35 deletions

View file

@ -91,6 +91,7 @@ class Callbacks:
# Remove the command prefix
cmd = msg[len(self.command_prefix) :]
try:
command = CommandFactory.create(
cmd,
self.client,
@ -102,6 +103,10 @@ class Callbacks:
event.event_id,
alert_event_id,
)
except TypeError as e:
logging.error(f"Unable to process the command '{cmd}': {e}")
return
await command.process()
async def invite(self, room: MatrixRoom, event: InviteMemberEvent) -> None:
@ -196,8 +201,10 @@ class Callbacks:
)
# Send a message acknowledging the reaction
cmd = f"ack {duration}"
try:
command = CommandFactory.create(
f"ack {duration}",
cmd,
self.client,
self.cache,
self.alertmanager,
@ -207,6 +214,10 @@ class Callbacks:
event.event_id,
alert_event_id,
)
except TypeError as e:
logging.error(f"Unable to process the command '{cmd}': {e}")
return
await command.process()
async def redaction(self, room: MatrixRoom, event: RedactionEvent) -> None:
@ -224,6 +235,7 @@ class Callbacks:
alert_event_id = self.cache[event.redacts]
try:
command = CommandFactory.create(
"unack",
self.client,
@ -235,6 +247,10 @@ class Callbacks:
event.redacts,
alert_event_id,
)
except TypeError as e:
logging.error(f"Unable to process the command 'unack': {e}")
return
await command.process()
async def decryption_failure(self, room: MatrixRoom, event: MegolmEvent) -> None:

View file

@ -278,7 +278,9 @@ class CommandFactory:
reacted_to_event_id: Optional[str] = None,
) -> BaseCommand:
if cmd.startswith("ack"):
assert reacted_to_event_id is not None
if reacted_to_event_id is None:
raise TypeError("Alert command must be in reply to an alert event.")
return AckAlertCommand(
client,
cache,
@ -291,7 +293,9 @@ class CommandFactory:
reacted_to_event_id,
)
elif cmd.startswith("unack") or cmd.startswith("nack"):
assert reacted_to_event_id is not None
if reacted_to_event_id is None:
raise TypeError("Alert command must be in reply to an alert event.")
return UnackAlertCommand(
client,
cache,