[mod_python] returning errors from python handler and ErrorDocument

Graham Dumpleton grahamd at dscpl.com.au
Sun Nov 27 16:44:43 EST 2005

AFAIK, the distinction is that using:

   req.status = apache.HTTP_UNAUTHORIZED
   return apache.OK

is for when you want to formulate your own response content. Ie.,  
override in the
handler what the error message displayed to the browser is, where  
"..." is replaced
by assignment to req.content_type and req.write() calls. Although, in  
practice, it
possibly should return apache.DONE and not apache.OK so as to avoid  
any later
handlers within the phase or later phases being run. Thus:

   req.status = apache.HTTP_UNAUTHORIZED
   req.content_type = 'text/plain'
   req.write('my error message')
   return apache.DONE

Raise an exception using apache.SERVER_RETURN should be used when you
want to again abort latter handlers in same phase or later phase but  
want Apache to then deal with the error response further by  
formatting a precanned
error message or triggering any handler associated with a  
ErrorDocument directive
or delivering back a static file associated with an ErrorDocument  


On 27/11/2005, at 3:47 AM, Jim Gallacher wrote:

> Nic Ferrier wrote:
>> I've got a python handler that does this:
>>    # something.py
>>    def handler(http):
>>       http.status = apache.UNAUTHORIZED
>>       return apache.OK
>> And in the apache config I've got something like:
>>    <Location /something>
>>    SetHandler python-program
>>    PythonHandler something
>>    </Location>
>>    ErrorDocument 401 /somepage.html
>> But the error document is not being served for requests to my
>> handler. I think it should because I'm returning 401.
>> Can someone enlighten me? Does the request processing just not work
>> like that?
> You would think that just setting the status would suffice but this  
> does not seem to be the way it works. Without actually checking I  
> imagine that the client will still see the req.status you set, plus  
> whatever content your handler dishes out, but you have not told  
> apache that you want to interupt processing have sling some  
> different content. You do this by raising an exception.
> from mod_python import apache
> def handler(req):
>     if unknown_user(req):
>         raise apache.SERVER_RETURN, apache.HTTP_UNAUTHORIZED
>     if  user_is_naughty(req):
>         raise apache.SERVER_RETURN, apache.HTTP_FORBIDDEN
>     if user_is_asking_for_something_silly(req):
>         raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
>     req.write("It's all good")
>     return apache.OK
> Jim
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list