<html>
<body>
<br>
Here's the next challenge in my ongoing quest to write a DBAPI
2.0-compliant abstraction layer.... wrapping exceptions.<br><br>
For this example, let's assume I am using mysqldb as the underlying
driver.<br><br>
<tt>class DriverBase(object):<br><br>
&nbsp;&nbsp;&nbsp; def __init__( self, paramstyle=None ):<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if paramstyle == None:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self.paramstyle =
'qmark'<x-tab>&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>#
Default public paramstyle<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self.paramstyle = paramstyle<br><br>
import MySQLdb as driver<br><br>
class Driver(DriverBase):<br><br>
&nbsp;&nbsp;&nbsp; name = 'mysqldb'<br><br>
&nbsp;&nbsp;&nbsp; def __init__( self, paramstyle=None ):<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self._driver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
driver<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>#
Link to the underlying MySQLdb driver namespace<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(Driver,
self).__init__(paramstyle)<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self.version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
driver.__version__<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self._paramstyle&nbsp;&nbsp;&nbsp; =
driver.paramstyle<x-tab>&nbsp;</x-tab># Underlying private
paramstyle<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
self.apilevel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = driver.apilevel<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.threadsafety&nbsp;&nbsp;
= driver.threadsafety<br><br>
</tt>The next thing I want to do is wrap the DPABI exceptions of the
underlying driver and raise my own equivalents ( which will handle<br>
exception arguments consistently... ) whenever they occur.&nbsp; Here is
the strategy that comes to mind, but I am wondering how the rest of you
might<br>
handle a case like this? <br><br>
# First define our own DBAPI exeptions... this example is limited to only
three for brevity.<br>
<tt>import exceptions<br>
class Error(exceptions.StandardError):<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>#
DAL-specific stuff<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>pass<br><br>
class DatabaseError(Error):<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>#
DAL-specific stuff<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>pass<br><br>
class InternalError(DatabaseError):<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>#
DAL-specific stuff<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>pass<br><br>
</tt>With the exceptions defined, I want to raise them whenever a wrapped
method call raises an exception in the<br>
underlying driver.&nbsp; Here is how I might do this with the connect()
function in the main driver ( this example returns<br>
a Connection object from the underlying driver... the final
implementation will wrap those objects as well. )<br><br>
<tt>def connect( **kwargs ):<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>try:<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>return
driver.connect(**kwargs)<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>except
driver.Error, args:<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>raise
Error, args<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>except
driver.DatabaseError, args:<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>raise
DatabaseError, args<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>except
driver.InternalError, args:<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>raise
InternalError, args<br><br>
</tt>Is this type of brute-force exception checking around every wrapped
method call necessary or am I missing a much simpler and elegant
solution?<br><br>
Thanks again for everyone's great feedback.<br><br>
<tt><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>Peter
Buschman<br>
</body>
</html>