Skip to content

Commit 778e44e

Browse files
committed
Improved error message for configuration files
1 parent ed1b34c commit 778e44e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

flask/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ def from_pyfile(self, filename):
116116
filename = os.path.join(self.root_path, filename)
117117
d = type(sys)('config')
118118
d.__file__ = filename
119-
execfile(filename, d.__dict__)
119+
try:
120+
execfile(filename, d.__dict__)
121+
except IOError, e:
122+
e.strerror = 'Unable to load configuration file (%s)' % e.strerror
123+
raise
120124
self.from_object(d)
121125

122126
def from_object(self, obj):

tests/flask_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,18 @@ def test_config_from_envvar(self):
10181018
finally:
10191019
os.environ = env
10201020

1021+
def test_config_missing(self):
1022+
app = flask.Flask(__name__)
1023+
try:
1024+
app.config.from_pyfile('missing.cfg')
1025+
except IOError, e:
1026+
msg = str(e)
1027+
assert msg.startswith('[Errno 2] Unable to load configuration '
1028+
'file (No such file or directory):')
1029+
assert msg.endswith("missing.cfg'")
1030+
else:
1031+
assert 0, 'expected config'
1032+
10211033

10221034
class SubdomainTestCase(unittest.TestCase):
10231035

0 commit comments

Comments
 (0)