From: Fredrik Tolf Date: Sun, 5 Feb 2012 15:44:08 +0000 (+0100) Subject: Use the env module to catch errors in the dispatcher. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=784d41f352527072fd9e26488f79e163868619b9;p=wrw.git Use the env module to catch errors in the dispatcher. --- diff --git a/wrw/dispatch.py b/wrw/dispatch.py index fe62df3..15ea99e 100644 --- a/wrw/dispatch.py +++ b/wrw/dispatch.py @@ -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: