Catch exceptions when parsing config file

This commit is contained in:
Michael Gummelt
2015-08-10 12:27:56 -07:00
parent 5daa9de46b
commit 9b6ae242df
3 changed files with 21 additions and 1 deletions

View File

@@ -0,0 +1,2 @@
header]
key=value

View File

@@ -440,6 +440,19 @@ def test_timeout(missing_env):
_unset_value('marathon.url', None, missing_env)
def test_parse_error():
env = os.environ.copy()
path = os.path.join('tests', 'data', 'config', 'parse_error.toml')
env['DCOS_CONFIG'] = path
assert_command(['dcos', 'config', 'show'],
returncode=1,
stderr=six.b(("Error parsing config file at [{}]: Found "
"invalid character in key name: ']'. "
"Try quoting the key name.\n").format(path)),
env=env)
def _fail_url_validation(command, key, value, env):
returncode_, stdout_, stderr_ = exec_command(
['dcos', 'config', command, key, value], env=env)

View File

@@ -2,6 +2,7 @@ import collections
import toml
from dcos import util
from dcos.errors import DCOSException
def load_from_path(path, mutable=False):
@@ -17,7 +18,11 @@ def load_from_path(path, mutable=False):
util.ensure_file_exists(path)
with util.open_file(path, 'r') as config_file:
toml_obj = toml.loads(config_file.read())
try:
toml_obj = toml.loads(config_file.read())
except Exception as e:
raise DCOSException(
'Error parsing config file at [{}]: {}'.format(path, e))
return (MutableToml if mutable else Toml)(toml_obj)