[Mpuls-commits] r2727 - in base/trunk: . mpulsweb/config

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri May 21 19:03:52 CEST 2010


Author: bh
Date: 2010-05-21 19:03:51 +0200 (Fri, 21 May 2010)
New Revision: 2727

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/config/middleware.py
Log:
* mpulsweb/config/middleware.py (SimpleExceptionLogger): New class
for very simple an more robust exception logging.
(make_app): Install SimpleExceptionLogger if the config option
mpuls.app.simple-exception-handler is true. The more sophisticated
pylons error handlers sometimes have problems with exceptions that
contain non-ascii characters in which case exceptions raised from
withing the error handler completely mask the original exception.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-05-21 16:36:52 UTC (rev 2726)
+++ base/trunk/ChangeLog	2010-05-21 17:03:51 UTC (rev 2727)
@@ -1,5 +1,15 @@
 2010-05-21  Bernhard Herzog  <bh at intevation.de>
 
+	* mpulsweb/config/middleware.py (SimpleExceptionLogger): New class
+	for very simple an more robust exception logging.
+	(make_app): Install SimpleExceptionLogger if the config option
+	mpuls.app.simple-exception-handler is true. The more sophisticated
+	pylons error handlers sometimes have problems with exceptions that
+	contain non-ascii characters in which case exceptions raised from
+	withing the error handler completely mask the original exception.
+
+2010-05-21  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/templates/administration/dialogs/failed_new_user.mako:
 	Removed.  Not used anymore.
 

Modified: base/trunk/mpulsweb/config/middleware.py
===================================================================
--- base/trunk/mpulsweb/config/middleware.py	2010-05-21 16:36:52 UTC (rev 2726)
+++ base/trunk/mpulsweb/config/middleware.py	2010-05-21 17:03:51 UTC (rev 2727)
@@ -21,6 +21,29 @@
 log = logging.getLogger(__name__)
 
 
+class SimpleExceptionLogger(object):
+
+    """Simply log any exception raised while a request is handled.
+
+    If an exception is raised during a request, the exception is logged
+    with log.exception and reraised without any further processing.
+    This can be used to make sure that the actual exception information
+    is logged even if the more sophisticated error handlers of
+    e.g. Pylons fail.  For that use-case it has to be as near to the
+    actual application in the application stack as possible.
+    """
+
+    def __init__(self, application):
+        self.application = application
+
+    def __call__(self, *args, **kw):
+        try:
+            return self.application(*args, **kw)
+        except:
+            log.exception("Exception while handling request")
+            raise
+
+
 class MyPylonsApp(PylonsApp):
 
     def find_controller(self, controller):
@@ -84,6 +107,18 @@
     # The Pylons WSGI app
     app = MyPylonsApp()
 
+    # Pylon's exception handler (at least the debug handler) sometimes
+    # has problems with exceptions whose messages are byte-strings
+    # containing non-ascii characters, which can happen when
+    # e.g. psycopg2 is used in a German locale.  To avoid loss of
+    # information, we put the SimpleExceptionLogger immediately around
+    # the pylons app.
+    if config.get("mpuls.app.simple-exception-handler", False):
+        log.debug("Using MPuls SimpleExceptionLogger")
+        app = SimpleExceptionLogger(app)
+    else:
+        log.debug("Not using MPuls SimpleExceptionLogger")
+
     # Routing/Session/Cache Middleware
     app = RoutesMiddleware(app, config['routes.map'])
     app = SessionMiddleware(app, config)



More information about the Mpuls-commits mailing list