[Xapian-devel] Error handling in the bindings

Richard Boulton richard at lemurconsulting.com
Sat Mar 3 01:05:05 GMT 2007


Currently, the SWIG generated bindings catch exceptions raised by Xapian 
and rethrow them using the standard SWIG error types: for example, if 
any Xapian::DatabaseError exception is raised, SWIG will use its
standard SWIG_IOError error type to report the error.

In Python, this leads to code like the following:

try:
     db.get_document(1)
except RuntimeError, e:
     if str(e).startswith('DocNotFoundError:'):
         # Handle a DocNotFoundError

(Incidentally, the error raised in python for this actually is a 
RuntimeError, not an IOError as I would have expected from reading the 
SWIG code, but that's not a major issue.)

For Python, it would be much nicer to have the full exception heirarchy 
of Xapian mirrored as Python classes, with the appropriate inheritance 
structure.  This is probably true for other languages too.  Looking at 
the code, I don't think it would take terribly long to sort this out for 
Python, but I haven't looked at what's needed for the other bindings we 
support.  It would certainly be much nicer to be able to write the above 
Python snippet as:

try:
     db.get_document(1)
except xapian.DocNotFoundError, e:
     # Handle a DocNotFoundError

In theory, the good thing about using the built in SWIG error handling 
system is that we don't have to write code for each language we support 
- but we already have conditional code for PHP for exceptions, so I 
don't feel that we'd be loosing much here.

The recent reworking of the way that the C++ code for the error classes 
is generated might make it possible to generate the SWIG interface code 
for the error classes for each language reasonably easily, and it would 
be nice to get this done before we release 1.0 if possible, so unless 
someone shouts up with a reason that it's not a good idea to try this, 
I'll try making a patch to fix up the Python exception handling next 
week, to see how feasible it is.

-- 
Richard



More information about the Xapian-devel mailing list