util:wsgi: Don't clobber `handler` when clearing StreamHandlers
[be.git] / libbe / util / wsgi.py
index eddf36fd03ee3f826a7c9eacc82e70770c3fe5db..dcddcf331601bdc5e8a3a525aa9302976d53cdbf 100644 (file)
@@ -58,12 +58,14 @@ try:
 except ImportError:
     OpenSSL = None
 
-
-import libbe.util.encoding
+import libbe
 import libbe.command
 import libbe.command.base
 import libbe.command.util
 import libbe.storage
+import libbe.util.encoding
+import libbe.util.http
+import libbe.util.id
 
 
 if libbe.TESTING == True:
@@ -306,7 +308,6 @@ class BEExceptionApp (WSGI_Middleware):
     """
     def __init__(self, *args, **kwargs):
         super(BEExceptionApp, self).__init__(*args, **kwargs)
-        self.http_user_error = 418
 
     def _call(self, environ, start_response):
         try:
@@ -315,9 +316,19 @@ class BEExceptionApp (WSGI_Middleware):
             raise libbe.util.wsgi.HandlerError(403, 'Read permission denied')
         except libbe.storage.NotWriteable as e:
             raise libbe.util.wsgi.HandlerError(403, 'Write permission denied')
-        except libbe.storage.InvalidID as e:
+        except (libbe.command.UsageError,
+                libbe.command.UserError,
+                OSError,
+                libbe.storage.ConnectionError,
+                libbe.util.http.HTTPError,
+                libbe.util.id.MultipleIDMatches,
+                libbe.util.id.NoIDMatches,
+                libbe.util.id.InvalidIDStructure,
+                libbe.storage.InvalidID,
+                ) as e:
+            msg = '{} {}'.format(type(e).__name__, format(e))
             raise libbe.util.wsgi.HandlerError(
-                self.http_user_error, 'InvalidID {}'.format(e))
+                libbe.util.http.HTTP_USER_ERROR, msg)
 
 
 class UppercaseHeaderApp (WSGI_Middleware):
@@ -676,6 +687,10 @@ class ServerCommand (libbe.command.base.Command):
             handler = logging.handlers.TimedRotatingFileHandler(
                 path, when='w6', interval=1, backupCount=4,
                 encoding=libbe.util.encoding.get_text_file_encoding())
+            while libbe.LOG.handlers:
+                h = libbe.LOG.handlers[0]
+                libbe.LOG.removeHandler(h)
+            libbe.LOG.addHandler(handler)
         else:
             handler = logging.StreamHandler(self.stdout)
         handler.setFormatter(logging.Formatter('%(message)s'))