oria general
oria general

Reputation: 273

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,

Upvotes: 7

Views: 29244

Answers (2)

lalit gangwar
lalit gangwar

Reputation: 401

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]))

Upvotes: 9

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

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...

Upvotes: 5

Related Questions