Skip to content

Commit

Permalink
Added a post-mortem pdb middleware. Works like the PostmortemDebuggin…
Browse files Browse the repository at this point in the history
…gHTTP server,

except that this does the debugging where it belongs, in a middleware, not in the
app and not in the server (yikes!).
  • Loading branch information
philikon committed Aug 23, 2007
1 parent 7808149 commit 0764cbc
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions z3c/evalexception.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
import zope.security.management
from paste.evalexception.middleware import EvalException

class ZopeEvalException(EvalException):
"""Wrapper around Paste's EvalException middleware that simply
tells zope.publisher to let exceptions propagate to the middleware."""

def __call__(self, environ, start_response):
environ['wsgi.handleErrors'] = False
return super(ZopeEvalException, self).__call__(environ, start_response)

def zope_eval_exception(app, global_conf):
return ZopeEvalException(app)

def PostMortemDebug(application):
"""Middleware that catches exceptions coming from a
zope.publisher-based application and invokes pdb's post-mortem
debugging facility."""
def middleware(environ, start_response):
environ['wsgi.handleErrors'] = False
try:
for chunk in application(environ, start_response):
yield chunk
except:
import sys, pdb
print "%s:" % sys.exc_info()[0]
print sys.exc_info()[1]
zope.security.management.restoreInteraction()
try:
pdb.post_mortem(sys.exc_info()[2])
raise
finally:
zope.security.management.endInteraction()
return middleware

def post_mortem_debug(app, global_conf):
return PostMortemDebug(app)

def TestApplication(environ, start_response):
"""A simple WSGI app that raises an exception for testing
purposes. Nothing to see here."""
raise RuntimeError('The test application is raising this.')
start_response('200 OK', [('Content-type', 'text/plain')])
yield "Test Application"

def test_application_factory(global_config):
return TestApplication

0 comments on commit 0764cbc

Please sign in to comment.