richbai90
richbai90

Reputation: 5204

unable to catch exception on connection pymysql

@classmethod
def getSwDataPass(self, uid, pw, dbname='swdata', _count=0):
    result = None

    if dbname == '':
        dbname = input('Could not find a database by the name swdata. Please provide the database name : ')
        if dbname == '' or dbname is None:
            print('There was a problem connecting to the database...exiting')
            sys.exit()
    try:
        connection = pymysql.connect(host='localhost',
                                     user=uid,
                                     password=pw,
                                     db='sw_config',
                                     port=5002,
                                     cursorclass=pymysql.cursors.DictCursor)

    except pymysql.err.OperationalError:
        print('Unable to make a connection to the mysql database. Please provide your swdata credentials')
        credentials = self.getrootpass(True)
        result = {"uid": credentials[0], "pwd": credentials[1]}
        continue

    if result is None:
        try:
            with connection.cursor() as cursor:
                cursor.execute("select uid, pwd, dbname from sw_databases where dbname = '{0}'".format(dbname))
                result = cursor.fetchone()
                if result is None:
                    connection.close()
                    if _count < 2:
                        self.getSwDataPass(uid, pw, '', _count + 1)

        finally:
            connection.close()
    return result

When I input invalid credentials for connecting to mysql I get an unhandled exception and the program exits with an error

Traceback (most recent call last):
  File "buildzapp.py", line 248, in <module>
    BuildZapp.main()
  File "buildzapp.py", line 96, in main
    zapp = BuildZapp(manifest, pw, uid, dbname)
  File "buildzapp.py", line 51, in __init__
    swdata_credentials = Utilities.getSwDataPass(self.conf_uid, self.conf_pw)
  File "C:\zapp builder\utilities.py", line 60, in getSwDataPass
    cursorclass=pymysql.cursors.DictCursor)
  File "C:\Python27\Lib\site-packages\pymysql\__init__.py", line 88, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 689, in __init__
    self.connect()
  File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 907, in connect
    self._request_authentication()
  File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 1115, in _request_authentication
    auth_packet = self._read_packet()
  File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 982, in _read_packet
    packet.check_error()
  File "C:\Python27\Lib\site-packages\pymysql\connections.py", line 394, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Python27\Lib\site-packages\pymysql\err.py", line 120, in raise_mysql_exception
    _check_mysql_exception(errinfo)
  File "C:\Python27\Lib\site-packages\pymysql\err.py", line 112, in _check_mysql_exception
    raise errorclass(errno, errorvalue)
pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: YES)")

I've event tried just an open except: just to test if I was catching the wrong error, no dice.

Upvotes: 2

Views: 5934

Answers (1)

Frederico Martins
Frederico Martins

Reputation: 1101

Try to assign pymysql.cursors.DictCursor to a variable before calling the function:

try:
    cursor = pymysql.cursors.DictCursor
    connection = pymysql.connect(host='localhost',
                                 user=uid,
                                 password=pw,
                                 db='sw_config',
                                 port=5002,
                                 cursorclass=cursor)

Upvotes: 1

Related Questions