From 4d8f4e64f114b9a67f435be24c9a3c4c1c72983c Mon Sep 17 00:00:00 2001 From: "FunnyMan3595 (Charlie Nolan)" Date: Sat, 9 Feb 2008 15:59:50 -0600 Subject: [PATCH] Wrapped command functions in a function that absorbs, logs, and tries to report errors. --- hal.py | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/hal.py b/hal.py index 5256eca..147e8a0 100755 --- a/hal.py +++ b/hal.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -import html, re, irclib, ircbot, thread, sys, atexit, bz2 +import html, re, irclib, ircbot, thread, sys, atexit, bz2, logging, traceback, time from cPickle import dumps as pickle, loads as depickle DEBUG = 0 irclib.DEBUG = DEBUG @@ -28,6 +28,33 @@ try: except IOError: pass +logging.getLogger().addHandler(logging.FileHandler("error.log")) + +class Buffer(object): + def __init__(self, prefix=""): + self.data = prefix + def write(self, unbuffered): + self.data += unbuffered + +def safe_call(func, args): + try: + func(*args) + except Exception, e: + if isinstance(e, SystemExit): + raise + timestamp = time.ctime() + " " + time.tzname[time.daylight] + buffer = Buffer("Exception in function %s at %s:\n" + % (func.__name__, timestamp)) + traceback.print_exc(file=buffer) + logging.getLogger().error(buffer.data) + # Try to report the error interactively. + if len(args) >= 2: + try: + reply(args[0], args[1], "Ow! Ohh, man, that didn't feel good. " \ + +"Somebody get FunnyMan, quick!") + except Exception: + pass + def resize(list_or_tuple, length): '''Creates a new list or tuple of length 'length', using as many elements from list_or_tuple as possible and right-padding with None. Does not modify @@ -80,18 +107,18 @@ whois_info = {} def got_command(who, where, command, args=""): if not command in commands: - command = 'raise_error' + command = 'raise error' args = "I don't understand that." waiting = waiting_commands.get(who, []) if not waiting: (required_perms, do_it) = commands[command] if where == private and required_perms <= Perms.owner: - do_it(who, where, args) + safe_call(do_it, (who, where, args)) elif required_perms <= Perms.voice and where in hal.channels: chan = hal.channels[where] if who in chan.voiceddict or who in chan.hopdict or who in chan.operdict \ or who in chan.admindict or who in chan.ownerdict: - do_it(who, where, args) + safe_call(do_it, (who, where, args)) else: return else: @@ -124,12 +151,18 @@ def get_perms(who, where): commands = {} -commands['raise_error'] = (Perms.voice, reply) +commands['raise error'] = (Perms.voice, reply) def ping(who, where, args): reply(who, where, "Pong!") commands['ping'] = (Perms.voice, ping) +class TestException(Exception): pass + +def error(who, where, args): + raise TestException, str(args) +commands['error'] = (Perms.ircop, error) + def join(who, where, args): hal.connection.join(where) commands['join'] = (Perms.op, join) @@ -288,11 +321,11 @@ def do_commands(who): user = whois_info[who] for where, command, args in waiting_commands[who]: if command not in commands: - command = 'raise_error' + command = 'raise error' args = "I don't understand that." (required_perms, do_it) = commands[command] if required_perms <= get_perms(who, where): - do_it(who, where, args) + safe_call(do_it, (who, where, args)) waiting_commands[who] = [] def got_whois(conn, event): -- 2.11.4.GIT