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,17 +91,22 @@ class Callbacks:
# Remove the command prefix
cmd = msg[len(self.command_prefix) :]
command = CommandFactory.create(
cmd,
self.client,
self.cache,
self.alertmanager,
self.config,
room,
event.sender,
event.event_id,
alert_event_id,
)
try:
command = CommandFactory.create(
cmd,
self.client,
self.cache,
self.alertmanager,
self.config,
room,
event.sender,
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,17 +201,23 @@ class Callbacks:
)
# Send a message acknowledging the reaction
command = CommandFactory.create(
f"ack {duration}",
self.client,
self.cache,
self.alertmanager,
self.config,
room,
event.sender,
event.event_id,
alert_event_id,
)
cmd = f"ack {duration}"
try:
command = CommandFactory.create(
cmd,
self.client,
self.cache,
self.alertmanager,
self.config,
room,
event.sender,
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,17 +235,22 @@ class Callbacks:
alert_event_id = self.cache[event.redacts]
command = CommandFactory.create(
"unack",
self.client,
self.cache,
self.alertmanager,
self.config,
room,
event.sender,
event.redacts,
alert_event_id,
)
try:
command = CommandFactory.create(
"unack",
self.client,
self.cache,
self.alertmanager,
self.config,
room,
event.sender,
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,