From 784d41f352527072fd9e26488f79e163868619b9 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 5 Feb 2012 16:44:08 +0100 Subject: [PATCH] Use the env module to catch errors in the dispatcher. --- wrw/dispatch.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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: -- 2.11.0