python: Handle errors when loading chained modules more properly.
[ashd.git] / python / ashd / wsgidir.py
index afd13f5..6bf00b6 100644 (file)
@@ -33,11 +33,13 @@ argument `.fpy=my.module.foohandler' can be given to pass requests for
 functions, you may want to use the getmod() function in this module.
 """
 
-import os, threading, types
+import sys, os, threading, types, logging, getopt
 import wsgiutil
 
 __all__ = ["application", "wmain", "getmod", "cachedmod", "chain"]
 
+log = logging.getLogger("wsgidir")
+
 class cachedmod(object):
     """Cache entry for modules loaded by getmod()
 
@@ -161,12 +163,20 @@ def wmain(*argv):
     Returns the `application' function. If any arguments are given,
     they are parsed according to the module documentation.
     """
-    ret = handler()
-    for arg in argv:
+    hnd = handler()
+    ret = hnd.handle
+
+    opts, args = getopt.getopt(argv, "V")
+    for o, a in opts:
+        if o == "-V":
+            import wsgiref.validate
+            ret = wsgiref.validate.validator(ret)
+
+    for arg in args:
         if arg[0] == '.':
             p = arg.index('=')
-            ret.addext(arg[1:p], arg[p + 1:])
-    return ret.handle
+            hnd.addext(arg[1:p], arg[p + 1:])
+    return ret
 
 def chain(env, startreq):
     """Chain-loading WSGI handler
@@ -184,7 +194,11 @@ def chain(env, startreq):
     object.
     """
     path = env["SCRIPT_FILENAME"]
-    mod = getmod(path)
+    try:
+        mod = getmod(path)
+    except Exception:
+        log.error("Exception occurred when loading %s" % path, exc_info=sys.exc_info())
+        return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "Could not load WSGI handler.")
     entry = None
     if mod is not None:
         mod.lock.acquire()