MessageParser: On syntax error, detail which action caused the error

This can help users debug it.
This commit is contained in:
Valentin Lorentz 2023-08-02 20:39:00 +02:00
parent bb3d456fdf
commit 71ae97ef5e
2 changed files with 10 additions and 6 deletions

View File

@ -128,7 +128,7 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
cursor.execute("UPDATE triggers SET usage_count=? WHERE regexp=?", (old_count + 1, regexp,)) cursor.execute("UPDATE triggers SET usage_count=? WHERE regexp=?", (old_count + 1, regexp,))
db.commit() 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.""" """Run a command from message, as if command was sent over IRC."""
try: try:
tokens = callbacks.tokenize(command, tokens = callbacks.tokenize(command,
@ -136,7 +136,8 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
except SyntaxError as e: except SyntaxError as e:
# Emulate what callbacks.py does # Emulate what callbacks.py does
self.log.debug('Error return: %s', utils.exnToString(e)) 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: try:
self.Proxy(irc.irc, msg, tokens) self.Proxy(irc.irc, msg, tokens)
except Exception as e: except Exception as e:
@ -200,15 +201,15 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
# Need a lambda to prevent re.sub from # Need a lambda to prevent re.sub from
# interpreting backslashes in the replacement # interpreting backslashes in the replacement
thisaction = re.sub(r'\$' + str(i+1), lambda _: match.group(i+1), thisaction) 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): if max_triggers != 0 and max_triggers == len(actions):
break break
if max_triggers != 0 and max_triggers == len(actions): if max_triggers != 0 and max_triggers == len(actions):
break break
for action in actions: for (regexp, action) in actions:
self._runCommandFunction(irc, msg, action) self._runCommandFunction(irc, msg, action, regexp)
def doPrivmsg(self, irc, msg): def doPrivmsg(self, irc, msg):
if not callbacks.addressed(irc, msg): #message is not direct command if not callbacks.addressed(irc, msg): #message is not direct command

View File

@ -89,7 +89,10 @@ class MessageParserTestCase(ChannelPluginTestCase):
def testSyntaxError(self): def testSyntaxError(self):
self.assertNotError(r'messageparser add "test" "echo foo \" bar"') self.assertNotError(r'messageparser add "test" "echo foo \" bar"')
self.feedMsg('test') 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): def testMatchedBackslashes(self):
# Makes sure backslashes in matched arguments are not interpreted # Makes sure backslashes in matched arguments are not interpreted