util:wsgi: add HandlerErrorApp to return HTTP errors.
authorW. Trevor King <wking@tremily.us>
Mon, 3 Sep 2012 13:34:35 +0000 (09:34 -0400)
committerW. Trevor King <wking@tremily.us>
Mon, 3 Sep 2012 13:34:35 +0000 (09:34 -0400)
libbe/util/wsgi.py

index 0a3ebf9df0edb6c5bc3ffd84ab955cd929237b20..0753295f476ad7597cf1db0056cf54ee3633dcf9 100644 (file)
@@ -267,6 +267,18 @@ class ExceptionApp (WSGI_Middleware):
             raise
 
 
+class HandlerErrorApp (WSGI_Middleware):
+    """Catch HandlerErrors and return HTTP error pages.
+    """
+    def _call(self, environ, start_response):
+        try:
+            return self.app(environ, start_response)
+        except HandlerError, e:
+            self.log_request(environ, status=str(e), bytes=0)
+            start_response('{} {}'.format(e.code, e.msg), e.headers)
+            return []
+
+
 class BEExceptionApp (WSGI_Middleware):
     """Translate BE-specific exceptions
     """
@@ -620,6 +632,7 @@ class ServerCommand (libbe.command.base.Command):
             'port':params['port'],
             }
         app = BEExceptionApp(app, logger=self.logger)
+        app = HandlerErrorApp(app, logger=self.logger)
         app = ExceptionApp(app, logger=self.logger)
         if params['ssl'] == True:
             details['protocol'] = 'HTTPS'