[mod_python] mod_python newbie help

Graham Dumpleton grahamd at dscpl.com.au
Mon Oct 3 16:59:53 EDT 2005

On 04/10/2005, at 4:18 AM, Jorey Bump wrote:

> El TuZa wrote:
>> Hi list, my question is about handling "compilation" errors (using
>> publisher). for example if someone calls a function with no params it
>> returns the error with all the python specifications about it, but i
>> just want to inform that person my own message or just call an error
>> function. Can this be done? thanks a lot
> You can suppress such errors by setting:
>  PythonDebug off

Also look at the ErrorDocument directive in Apache for a way of
triggering off special code to generate customised error pages for
different Apache error types.

> and/or use conventional Python exceptions to handle errors:
> try:
>     a = req.form['myvar']
> except KeyError, key:
>     return '<h1>No form variable by that name: %s</h1>' % (key,)

This unfortunately doesn't help with mod_python.publisher unless you are
prepared to throw away its abilities to automatically decode form
parameters into function arguments.

The closest you will get if you want to preserve this automatic form
argument decoding is to wrap the invocation of mod_python.publisher
within your own handler and for it to try and attempt to catch the
exception type which represents the missing argument. That is, instead

   PythonHandler mod_python.publisher


   PythonHandler myhandler

where "myhandler" is defined as:

   import mod_python.publisher

   def handler(req):
       return mod_python.publisher.handler(req)
     except KeyError, key
       # Generate special exception page.

The problem with this is that KeyError may have been generated due to
some totally different issue other than a missing form argument. Thus,
to make it do what is intended, you would need to look at traceback
information with the exception and identify if the exception was raised
in the mod_python.publisher form processing code.


More information about the Mod_python mailing list