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