|Author:||Ian Bicking <email@example.com>|
|Discussions-To:||Python Web-SIG <firstname.lastname@example.org>|
|Created:||13 Nov 2006|
WSGI applications are generally not supposed to raise exceptions, instead handling their own errors (possibly returning a 500 Server Error response). But in some context it is desired that unexpected exceptions be allowed to bubble up. This specification defines a key to set in this circumstance.
When in a testing context it is undesirable for an application to handle its own errors. Typically the test framework is better at handling the errors, either through error formatting or by dropping into a debugger like pdb.
Additionally when an exception catcher is installed in a stack, ideally it will be used for all exceptions. This allows for centralized configuration (for example, when emails are sent when errors occur). Dynamically disabling any other exception catchers is often ideal in this situation.
An exception catcher should check for x-wsgiorg.throw_errors. If it is true, it should not try to catch exceptions. This need only be checked as the application is being entered, it should not be checked later. Applications should not try to set this to effect middleware that wraps them, only to effect applications they may call.
A simple exception catcher:
class ExceptionCatch(object): def __init__(self, app): self.app = app def __call__(self, environ, start_response): if environ.get('x-wsgiorg.throw_errors'): return self.app(environ, start_response) try: return self.app(environ, start_response) except: import sys, traceback, StringIO exc_info = sys.exc_info() start_response('500 Server Error', [('content-type', 'text/plain')], exc_info=exc_info) out = StringIO.StringIO() traceback.print_exc(file=out) return [out.getvalue()]
WebTest sets a key (paste.throw_errors) during debugging, which allows it to do functional testing of applications that have the paste.exceptions middleware applied to them (that middleware looks for the key and disables itself per-request when it sees it).
Zope 2 has its own flag on the (non-WSGI) request to do this, showing substantial history for this technique. Zope 3 uses something like wsgi.handleErrors in the WSGI environ to the same effect (it shouldn’t be using wsgi., but it does).