Just realise that this code will only work if your WSGI application is return an inferable such as a list.
It will not have the intended affect if your WSGI application is a generator. This is because the actual body of the WSGI application will only be called on the first attempt by the WSGI server to get response content from the WSGI application. This would occur after the 'application' function had returned. Things start to get a lot more complicated at that point as any error catching middleware would have to also consume the response to pass it on to catch errors. It would also need to track whether it has already sent and partial response content and not shove the details of an error after that partial response, but simply log it. For an existing error catching middleware that handles all this properly see details in: http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Error_Catching_Middleware Graham On 19/02/2015, at 4:03 AM, Alexander Sh <[email protected]> wrote: > I've just realized, that a bypass is possible to achieve the goal I need > without redefining sys.excepthook: > > def actual_application(environ, start_response): > # Real WSGI application goes here > start_response('200 OK', [('Content-Type', 'text/plain')]) > return ['OK'] > > def error(environ, start_response): > # Format a trace and a debug info to send it to a browser here > start_response('200 OK', [('Content-Type', 'text/plain')]) > return ['Error'] > > def application(environ, start_response): > # And the mod_wsgi application hook should consist of the only > try...except block > try: > return actual_application(environ, start_response) > except: > return error(environ, start_response) > > But the question why I cannot redefine sys.excepthook successfully is still > unanswered. > > On Wednesday, February 18, 2015 at 10:03:48 AM UTC+3, Alexander Sh wrote: > > 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: > > def application(environ, start_response): > def exception(etype, evalue, trace): > start_response('200 OK', [('Content-Type', 'text/plain')]) > return ['Error'] > > sys.excepthook = exception > > ... > > > 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? > > > -- > You received this message because you are subscribed to the Google Groups > "modwsgi" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/modwsgi. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "modwsgi" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/modwsgi. For more options, visit https://groups.google.com/d/optout.
