From 58ee5c4ab4ce9be692277b520de72735494bd9f7 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 16 Jul 2012 08:11:20 +0200 Subject: [PATCH] python: Handle errors when loading chained modules more properly. --- python/ashd/wsgidir.py | 10 ++++++++-- python3/ashd/wsgidir.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/python/ashd/wsgidir.py b/python/ashd/wsgidir.py index 2da3a94..6bf00b6 100644 --- a/python/ashd/wsgidir.py +++ b/python/ashd/wsgidir.py @@ -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, getopt +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() @@ -192,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() diff --git a/python3/ashd/wsgidir.py b/python3/ashd/wsgidir.py index 31afd3b..c2efcca 100644 --- a/python3/ashd/wsgidir.py +++ b/python3/ashd/wsgidir.py @@ -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, importlib, getopt +import sys, os, threading, types, logging, importlib, getopt from . import wsgiutil __all__ = ["application", "wmain", "getmod", "cachedmod", "chain"] +log = logging.getLogger("wsgidir") + class cachedmod(object): """Cache entry for modules loaded by getmod() @@ -180,7 +182,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: with mod.lock: -- 2.11.0