Installation guide
10. mxODBC Exceptions and Error Handling
10.4 Error Handlers
If you want to provide your own error handler, e.g. to mask database warnings or
debug connection setups, you can do so by assigning to the
.errorhandler
attribute of connections and cursors or passing a callback function to the
connection constructors at connection creation time using the
errorhandler
keyword argument.
Error handlers are inherited from connections to cursors, so it normally suffices to
set an error handler on the connection object to have it take affect for all
subsequently created cursors.
Cursors created prior to setting the error handler on the connection will not see
or use the new error handler.
Error handler signature
An error handler has to be a callable object taking the arguments (connection,
cursor, errorclass, errorvalue)
where connection is a reference to the
connection (or
None in case the connection has not yet been setup), cursor a
reference to the cursor (or
None in case the error does not apply to a cursor or the
cursor has not yet been setup),
errorclass is an error class which to instantiate
using
errorvalue as construction argument.
Return values are currently not defined.
This a typical error handler function:
from mx.ODBC.Windows import Warning
# Error handler function
def myerrorhandler(connection, cursor, errorclass, errorvalue):
if issubclass(errorclass, Warning):
print ('Ignoring warning %s' % errorvalue)
else:
raise errorclass(*errorvalue)
Default error handler
The default mxODBC error handler will append the tuple (errorclass,
errorvalue)
to the .messages list of the cursor or connection (if cursor is None)
and then raise the exception by instantiating
errorclass with errorvalue.
Error processing
Note that only database and ODBC driver/manager related errors are processed
through the error handlers. Other errors such as mxODBC internal or
AttributeErrors are not processed by these handlers.
161