]> jfr.im git - erebus.git/commitdiff
add checking for sql errors
authorzonidjan <redacted>
Sat, 8 Mar 2014 19:55:21 +0000 (13:55 -0600)
committerzonidjan <redacted>
Sat, 8 Mar 2014 19:55:21 +0000 (13:55 -0600)
queries need to be repeated if MyCursor().execute() returns false. That has not been done.

bot.py
erebus.py
modules/eval.py

diff --git a/bot.py b/bot.py
index 1c8aa02a4b26c8bf68355e24e75859c2f270b4d0..2c214c746a4b35729d14dab22b9c14bab13cb55a 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -16,11 +16,10 @@ class Bot(object):
                self.realname = realname
 
                curs = self.parent.db.cursor()
-               curs.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self.nick,))
-               chansres = curs.fetchall()
-               curs.close()
-
-               self.chans = [self.parent.newchannel(self, row['chname']) for row in chansres]
+               if curs.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self.nick,)):
+                       chansres = curs.fetchall()
+                       curs.close()
+                       self.chans = [self.parent.newchannel(self, row['chname']) for row in chansres]
 
                self.conn = BotConnection(self, bind, server, port)
        def connect(self):
index d0d41a713a6222b1f17d489ce6ef97750e4aa55d..d96ae0becb650b4a93cca93c7b99aa247362ee0c 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -5,7 +5,7 @@
 
 #TODO: tons
 
-import os, sys, select, MySQLdb, MySQLdb.cursors
+import os, sys, select, MySQLdb, MySQLdb.cursors, time
 import bot, config, ctlmod
 
 class Erebus(object):
@@ -38,10 +38,12 @@ class Erebus(object):
                                self.glevel = -1
                        else:
                                c = main.db.cursor()
-                               c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,))
-                               row = c.fetchone()
-                               if row is not None:
-                                       self.glevel = row['level']
+                               if c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,)):
+                                       row = c.fetchone()
+                                       if row is not None:
+                                               self.glevel = row['level']
+                                       else:
+                                               self.glevel = 0
                                else:
                                        self.glevel = 0
                        return self.glevel
@@ -65,11 +67,11 @@ class Erebus(object):
                        self.ops = []
 
                        c = main.db.cursor()
-                       c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,))
-                       row = c.fetchone()
-                       while row is not None:
-                               self.levels[row['user']] = row['level']
+                       if c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,)):
                                row = c.fetchone()
+                               while row is not None:
+                                       self.levels[row['user']] = row['level']
+                                       row = c.fetchone()
 
 
                def levelof(self, auth):
@@ -83,8 +85,11 @@ class Erebus(object):
                        auth = auth.lower()
                        if savetodb:
                                c = main.db.cursor()
-                               c.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level))
-                       self.levels[auth] = level
+                               if c.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level)):
+                                       self.levels[auth] = level
+                                       return True
+                               else:
+                                       return False
 
                def userjoin(self, user, level=None):
                        if user not in self.users: self.users.append(user)
@@ -205,16 +210,13 @@ class MyCursor(MySQLdb.cursors.DictCursor):
                        super(self.__class__, self).execute(*args, **kwargs)
                except MySQLdb.MySQLError as e:
                        print "[SQL] [!] MySQL error! %r" % (e)
-                       try:
-                               dbsetup()
-                               super(self.__class__, self).execute(*args, **kwargs)
-                       except MySQLdb.MySQLError as e:
-                               print "[SQL] [!!!] DOUBLE MySQL error! %r" % (e)
-                               print "* [!!!] Giving up."
-                               sys.exit()
+                       dbsetup()
+                       return False
+               return True
 
 
 def dbsetup():
+       main.db = None
        main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MyCursor)
 
 def setup():
@@ -230,11 +232,11 @@ def setup():
 
        dbsetup()
        c = main.db.cursor()
-       c.execute("SELECT nick, user, bind FROM bots WHERE active = 1")
-       rows = c.fetchall()
-       c.close()
-       for row in rows:
-               main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname)
+       if c.execute("SELECT nick, user, bind FROM bots WHERE active = 1"):
+               rows = c.fetchall()
+               c.close()
+               for row in rows:
+                       main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname)
        main.connectall()
 
 def loop():
index 75361980609624a0a162c85b8a882896bd74b144..795c72a96376834c6172e11ce6b6750f41cb6b10 100644 (file)
@@ -26,6 +26,7 @@ def cmd_eval(bot, user, chan, realtarget, *args):
        else: replyto = user
 
        try: ret = eval(' '.join(args))
+       except SystemExit: raise
        except: bot.msg(replyto, "Error (%s): %s" % (sys.exc_info()[0], sys.exc_info()[1]))
        else: bot.msg(replyto, "Done: %r" % (ret))
 
@@ -37,5 +38,6 @@ def cmd_exec(bot, user, chan, realtarget, *args):
        else: replyto = user
 
        try: exec ' '.join(args)
+       except SystemExit: raise
        except: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
        else: bot.msg(replyto, "Done.")