Use the env module to catch errors in the dispatcher.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Feb 2012 15:44:08 +0000 (16:44 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Feb 2012 15:44:08 +0000 (16:44 +0100)
wrw/dispatch.py

index fe62df3..15ea99e 100644 (file)
@@ -1,3 +1,6 @@
+import sys, traceback
+import env
+
 __all__ = ["restart"]
 
 class restart(Exception):
@@ -39,7 +42,20 @@ class iterproxy(object):
         if hasattr(self.bk, "close"):
             self.bk.close()
 
+def defaulterror(req, excinfo):
+    import resp
+    traceback.print_exception(*excinfo)
+    raise resp.httperror(500)
+
+def wraphandler(handler, excinfo):
+    def wrapped(req):
+        return handler(req, excinfo)
+    return wrapped
+
+errorhandler = env.var(defaulterror)
+
 def handle(req, startreq, handler):
+    eh = errorhandler.val
     try:
         resp = [""]
         while True:
@@ -48,6 +64,11 @@ def handle(req, startreq, handler):
                 break
             except restart, i:
                 handler = i.handle
+            except Exception, i:
+                if eh is None:
+                    raise
+                handler = wraphandler(eh, sys.exc_info())
+                eh = None
         req.commit(startreq)
         return resp
     finally: