From 778e44e39eb42ff6cbee376ce40d1a52a25cb0ce Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 30 Jul 2010 00:03:06 +0200 Subject: [PATCH] Improved error message for configuration files --- flask/config.py | 6 +++++- tests/flask_tests.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/flask/config.py b/flask/config.py index cf9ad541..a27b362d 100644 --- a/flask/config.py +++ b/flask/config.py @@ -116,7 +116,11 @@ class Config(dict): filename = os.path.join(self.root_path, filename) d = type(sys)('config') d.__file__ = filename - execfile(filename, d.__dict__) + try: + execfile(filename, d.__dict__) + except IOError, e: + e.strerror = 'Unable to load configuration file (%s)' % e.strerror + raise self.from_object(d) def from_object(self, obj): diff --git a/tests/flask_tests.py b/tests/flask_tests.py index 93bdfef7..010bc42e 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -1018,6 +1018,18 @@ class ConfigTestCase(unittest.TestCase): finally: os.environ = env + def test_config_missing(self): + app = flask.Flask(__name__) + try: + app.config.from_pyfile('missing.cfg') + except IOError, e: + msg = str(e) + assert msg.startswith('[Errno 2] Unable to load configuration ' + 'file (No such file or directory):') + assert msg.endswith("missing.cfg'") + else: + assert 0, 'expected config' + class SubdomainTestCase(unittest.TestCase):