From 1bbb64618dd7f33f8432d5dea618601d49cec7b0 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Wed, 22 Feb 2017 22:08:04 -0600 Subject: [PATCH] bot: capture exceptions, try to report on them it beats crashing, probably --- ircbot/bot.py | 85 +++++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/ircbot/bot.py b/ircbot/bot.py index e01b82a..c6cb81c 100644 --- a/ircbot/bot.py +++ b/ircbot/bot.py @@ -144,51 +144,56 @@ class DrReactor(irc.client.Reactor): Also supports regex handlers. """ - log.debug("EVENT: e[%s] s[%s] t[%s] a[%s]", event.type, event.source, - event.target, event.arguments) + try: + log.debug("EVENT: e[%s] s[%s] t[%s] a[%s]", event.type, event.source, + event.target, event.arguments) - self.try_recursion(connection, event) + self.try_recursion(connection, event) - # only do aliasing for pubmsg/privmsg - if event.type in ['pubmsg', 'privmsg']: - what = event.arguments[0] - log.debug("checking for alias for %s", what) + # only do aliasing for pubmsg/privmsg + if event.type in ['pubmsg', 'privmsg']: + what = event.arguments[0] + log.debug("checking for alias for %s", what) - for alias in Alias.objects.all(): - repl = alias.replace(what) - if repl: - # we found an alias for our given string, doing a replace - event.arguments[0] = repl - # recurse, in case there's another alias in this one - return self._handle_event(connection, event) + for alias in Alias.objects.all(): + repl = alias.replace(what) + if repl: + # we found an alias for our given string, doing a replace + event.arguments[0] = repl + # recurse, in case there's another alias in this one + return self._handle_event(connection, event) - with self.mutex: - # doing regex version first as it has the potential to be more specific - log.debug("checking regex handlers for %s", event.type) - matching_handlers = sorted( - self.regex_handlers.get("all_events", []) + - self.regex_handlers.get(event.type, []) - ) - log.debug("got %d", len(matching_handlers)) - for handler in matching_handlers: - log.debug("checking %s vs. %s", handler, event.arguments) - for line in event.arguments: - match = re.search(handler.regex, line) - if match: - log.debug("match!") - result = handler.callback(connection, event, match) - if result == "NO MORE": - return + with self.mutex: + # doing regex version first as it has the potential to be more specific + log.debug("checking regex handlers for %s", event.type) + matching_handlers = sorted( + self.regex_handlers.get("all_events", []) + + self.regex_handlers.get(event.type, []) + ) + log.debug("got %d", len(matching_handlers)) + for handler in matching_handlers: + log.debug("checking %s vs. %s", handler, event.arguments) + for line in event.arguments: + match = re.search(handler.regex, line) + if match: + log.debug("match!") + result = handler.callback(connection, event, match) + if result == "NO MORE": + return - matching_handlers = sorted( - self.handlers.get("all_events", []) + - self.handlers.get(event.type, []) - ) - for handler in matching_handlers: - log.debug("not-match") - result = handler.callback(connection, event) - if result == "NO MORE": - return + matching_handlers = sorted( + self.handlers.get("all_events", []) + + self.handlers.get(event.type, []) + ) + for handler in matching_handlers: + log.debug("not-match") + result = handler.callback(connection, event) + if result == "NO MORE": + return + except Exception as ex: + log.error("caught exception!") + log.exception(ex) + connection.privmsg(event.target, str(ex)) def try_recursion(self, connection, event): """Scan message for subcommands to execute and use as part of this command.