NeDark
NeDark

Reputation: 1294

How to access error code in MySQL python library exceptions

I use the code from this question to catch operational errors: Python-MySQLdb, how do you access the exception error-code in `OperationalError`?

try:
            # Adding field 'Bug.bize_size_tag_name'
            db.add_column('search_bug', 'bize_size_tag_name', orm['search.bug:bize_size_tag_name'])
except MySQLdb.OperationalError, errorCode:
            if errorCode[0] == 1060:
                pass
            else:
                raise

However I cannot access the error code. I get error:

  File "main.py", line 835, in db_execute
    if errorCode[0] == 1213:
TypeError: 'OperationalError' object does not support indexing

Upvotes: 0

Views: 448

Answers (1)

NeDark
NeDark

Reputation: 1294

Thanks to @roganjosh comment to question I discovered the attributes of OperationalError:

['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'with_traceback']

The error code was in attribute args[0]

Upvotes: 1

Related Questions