Assigning a function to sys.excepthook doesn't work in WSGI

tak.govoril at gmail.com tak.govoril at gmail.com
Wed Feb 18 10:08:21 EST 2015


I want to generate an html page with http response status '200 OK' in case of an uncaught exception in my wsgi application, instead of web server's standard '500 Internal Server Error' response for such case. To do so, I've made the following sample code:

import sys

def application(environ, start_response):
    def exception(etype, evalue, trace):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return ['Error']

    sys.excepthook = exception

    1/0

    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['OK']

But the function 'exception' is never called, and a standard '500 Internal Server Error' response is still generated by server in case of an uncaught exception.

I looked through the documentation, but unable to find the answer. Are there any ways to handle uncaught by try..except exceptions under mod_wsgi?



More information about the Python-list mailing list