Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling PyMySql exceptions - Best Practices

My question regards exception best practices. I'll present my question on a specific case with PyMySQL but it regards errors handling in general. I am using PyMySQL and out of the many possible exceptions, there is one I want to deal with in a specific manner. "Duplicate" exception.

pymysql maps mysql errors to python errors according to the following table:

_map_error(ProgrammingError, ER.DB_CREATE_EXISTS, ER.SYNTAX_ERROR,
       ER.PARSE_ERROR, ER.NO_SUCH_TABLE, ER.WRONG_DB_NAME,
       ER.WRONG_TABLE_NAME, ER.FIELD_SPECIFIED_TWICE,
       ER.INVALID_GROUP_FUNC_USE, ER.UNSUPPORTED_EXTENSION,
       ER.TABLE_MUST_HAVE_COLUMNS, ER.CANT_DO_THIS_DURING_AN_TRANSACTION)
_map_error(DataError, ER.WARN_DATA_TRUNCATED, ER.WARN_NULL_TO_NOTNULL,
       ER.WARN_DATA_OUT_OF_RANGE, ER.NO_DEFAULT, ER.PRIMARY_CANT_HAVE_NULL,
       ER.DATA_TOO_LONG, ER.DATETIME_FUNCTION_OVERFLOW)
_map_error(IntegrityError, ER.DUP_ENTRY, ER.NO_REFERENCED_ROW,
       ER.NO_REFERENCED_ROW_2, ER.ROW_IS_REFERENCED, ER.ROW_IS_REFERENCED_2,
       ER.CANNOT_ADD_FOREIGN, ER.BAD_NULL_ERROR)
_map_error(NotSupportedError, ER.WARNING_NOT_COMPLETE_ROLLBACK,
       ER.NOT_SUPPORTED_YET, ER.FEATURE_DISABLED, ER.UNKNOWN_STORAGE_ENGINE)
_map_error(OperationalError, ER.DBACCESS_DENIED_ERROR, ER.ACCESS_DENIED_ERROR,
       ER.CON_COUNT_ERROR, ER.TABLEACCESS_DENIED_ERROR,
       ER.COLUMNACCESS_DENIED_ERROR)

I want to specifically catch ER.DUP_ENTRY but I only know how to catch IntegrityError and that leads to redundant cases within my exception catch.

cur.execute(query, values)
except IntegrityError as e:
     if e and e[0] == PYMYSQL_DUPLICATE_ERROR:
          handel_duplicate_pymysql_exception(e, func_a)
     else:
          handel_unknown_pymysql_exception(e, func_b)
except Exception as e:
     handel_unknown_pymysql_exception(e, func_b)

Is there a way to simply catch only ER.DUP_ENTRY some how? looking for something like:

except IntegrityError.DUP_ENTRY as e:
     handel_duplicate_pymysql_exception(e, func_a)

Thanks in advance for your guidance,

like image 926
oria general Avatar asked Dec 27 '16 11:12

oria general


People also ask

How do I handle exceptions in MySQL?

MySQL provides a handler to handle the exceptions in the stored procedures. You can handle these exceptions by declaring a handler using the MySQL DECLARE ... HANDLER Statement.

What is PyMySQL?

PyMySQL is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb.

Where do you handle exceptions in Python?

In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.


2 Answers

there is very generic way to use pymysql error handling. I am using this for sqlutil module. This way you can catch all your errors raised by pymysql without thinking about its type.

try:
    connection.close()
    print("connection closed successfully")
except pymysql.Error as e:
    print("could not close connection error pymysql %d: %s" %(e.args[0], e.args[1]))
like image 158
lalit gangwar Avatar answered Oct 06 '22 01:10

lalit gangwar


You cannot specify an expect clause based on an exception instance attribute obviously, and not even on an exception class attribute FWIW - it only works on exception type.

A solution to your problem is to have two nested try/except blocks, the inner one handling duplicate entries and re-raising other IntegrityErrors, the outer one being the generic case:

try:
    try:
        cur.execute(query, values)
    except IntegrityError as e:
        if e.args[0] == PYMYSQL_DUPLICATE_ERROR:
            handle_duplicate_pymysql_exception(e, func_a)
        else:
            raise

except Exception as e:
    handle_unknown_pymysql_exception(e, func_b)

Whether this is better than having a duplicate call to handle_unknown_pymysql_exception is up to you...

like image 44
bruno desthuilliers Avatar answered Oct 06 '22 00:10

bruno desthuilliers