pythonsqlalchemypython-db-api

Identifying sqlalchemy.exc.OperationalError


I'm trying to catch mysql/sqlalchemy OperationalErrors and replace handle access denied (1045) differently from connection refused (2003)

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user … (Background on this error at: http://sqlalche.me/e/e3q8)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") (Background on this error at: http://sqlalche.me/e/e3q8)

I just can't seem to find any documentation on how to tell these apart programmatically. I dived into the sources and thought I could check the value of err.orig.original_exception.errno but that was not the case.

Edit: err.orig doesn't seem to be defined for access denied which might be a bug.

try:
  engine.scalar(select([1]))
except sqlalchemy.exc.OperationalError as err:
  if err_______:
    print("Access Denied")
  elifif err_______:
    print("Connection Refused")
  else:
    raise

Solution

  • After some more research, I found the mysql error code to be in err.orig.args[0]. So the Answer is:

    try:
      engine.scalar(select([1]))
    except sqlalchemy.exc.OperationalError as err:
      if err.orig.args[0]==1045:
        print("Access Denied")
      elif err.orig.args[0]==2003:
        print("Connection Refused")
      else:
        raise