diff tincan.py @ 70:a78c74c73d98 draft

Get rid of bogus "None" messages in the standard error pages.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:13:46 -0700
parents c168aad7a731
children
line wrap: on
line diff
--- a/tincan.py	Mon Jul 15 08:13:51 2019 -0700
+++ b/tincan.py	Mon Jul 15 13:13:46 2019 -0700
@@ -221,6 +221,17 @@
 
 # U t i l i t i e s
 
+def _hterror(**kwargs):
+    """
+    Make a suitable bottle.HttpError object, with a message that is
+    always meaningful.
+    """
+    if "status" not in kwargs:
+        raise ValueError("status argument is mandatory")
+    if "body" not in kwargs:
+        kwargs["body"] = "No further details available."
+    return bottle.HTTPError(**kwargs)
+
 def _normpath(base, unsplit):
     """
     Split, normalize and ensure a possibly relative path is absolute. First
@@ -482,12 +493,12 @@
                 mtime = os.fstat(fp.fileno()).st_mtime
                 bytes = fp.read()
         except FileNotFoundError as e:
-            return bottle.HTTPError(status=404, exception=e)
+            return _hterror(status=404, exception=e)
         except PermissionError as e:
-            return bottle.HTTPError(status=403, exception=e)
+            return _hterror(status=403, exception=e)
         except OSError as e:
             self.logger.exception("unexpected exception reading %r", self._fspath)
-            return bottle.HTTPError(status=500, exception=e)
+            return _hterror(status=500, exception=e)
         # Establish preliminary standard headers.
         headers = {
             "Content-Type": self._type,
@@ -539,7 +550,7 @@
             # this will cause a "Critical error while processing request"
             # page to be displayed, and any installed error pages to be
             # ignored.
-            raise bottle.HTTPError(status=500, exception=e)
+            raise _hterror(status=500, exception=e)
 
 class _TinCanRoute(_TinCanBaseRoute):
     """
@@ -791,10 +802,11 @@
             return e
         except Exception as e:
             self.logger.exception("%s: unexpected exception", self._urlpath)
-            raise bottle.HTTPError(status=500, exception=e)
+            raise _hterror(status=500, exception=e)
         if target is None:
-            self.logger.error("%s: unexpected null target", self._urlpath)
-            raise bottle.HTTPError(status=500, exception=TinCanError(message))
+            message = "{0}: unexpected null target".format(self._urlpath)
+            self.logger.error(message)
+            raise _hterror(status=500, exception=TinCanError(message))
         # We get here if we are doing a server-side programmatic
         # forward.
         environ = bottle.request.environ
@@ -803,8 +815,9 @@
         if _FLOOP not in environ:
             environ[_FLOOP] = set([self._urlpath])
         elif target in environ[_FLOOP]:
-            self.logger.error("%s: forward loop detected", environ[_FORIG])
-            raise bottle.HTTPError(status=500, exception=TinCanError(message))
+            message = "{0}: forward loop detected".format(environ[_FORIG])
+            self.logger.error(message)
+            raise _hterror(status=500, exception=TinCanError(message))
         environ[_FLOOP].add(target)
         environ['bottle.raw_path'] = target
         environ['PATH_INFO'] = urllib.parse.quote(target)