From 71ae97ef5e4f211c9c36033d1b2c8607de7e4f8d Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Wed, 2 Aug 2023 20:39:00 +0200 Subject: [PATCH] MessageParser: On syntax error, detail which action caused the error This can help users debug it. --- plugins/MessageParser/plugin.py | 11 ++++++----- plugins/MessageParser/test.py | 5 ++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/MessageParser/plugin.py b/plugins/MessageParser/plugin.py index 981f3a70b..baca48ad4 100644 --- a/plugins/MessageParser/plugin.py +++ b/plugins/MessageParser/plugin.py @@ -128,7 +128,7 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler): cursor.execute("UPDATE triggers SET usage_count=? WHERE regexp=?", (old_count + 1, regexp,)) db.commit() - def _runCommandFunction(self, irc, msg, command): + def _runCommandFunction(self, irc, msg, command, action_name): """Run a command from message, as if command was sent over IRC.""" try: tokens = callbacks.tokenize(command, @@ -136,7 +136,8 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler): except SyntaxError as e: # Emulate what callbacks.py does self.log.debug('Error return: %s', utils.exnToString(e)) - irc.error(str(e)) + irc.error(format('%s, in %r (triggered by %r)', + e, command, action_name)) try: self.Proxy(irc.irc, msg, tokens) except Exception as e: @@ -200,15 +201,15 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler): # Need a lambda to prevent re.sub from # interpreting backslashes in the replacement thisaction = re.sub(r'\$' + str(i+1), lambda _: match.group(i+1), thisaction) - actions.append(thisaction) + actions.append((regexp, thisaction)) if max_triggers != 0 and max_triggers == len(actions): break if max_triggers != 0 and max_triggers == len(actions): break - for action in actions: - self._runCommandFunction(irc, msg, action) + for (regexp, action) in actions: + self._runCommandFunction(irc, msg, action, regexp) def doPrivmsg(self, irc, msg): if not callbacks.addressed(irc, msg): #message is not direct command diff --git a/plugins/MessageParser/test.py b/plugins/MessageParser/test.py index f65c7d0bd..6aeb4ddce 100644 --- a/plugins/MessageParser/test.py +++ b/plugins/MessageParser/test.py @@ -89,7 +89,10 @@ class MessageParserTestCase(ChannelPluginTestCase): def testSyntaxError(self): self.assertNotError(r'messageparser add "test" "echo foo \" bar"') self.feedMsg('test') - self.assertResponse(' ', 'Error: No closing quotation') + self.assertResponse( + ' ', + r"""Error: No closing quotation, in """ + r"""'echo foo " bar' (triggered by 'test')""") def testMatchedBackslashes(self): # Makes sure backslashes in matched arguments are not interpreted