Catch exceptions when parsing config file
This commit is contained in:
2
cli/tests/data/config/parse_error.toml
Normal file
2
cli/tests/data/config/parse_error.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
header]
|
||||
key=value
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user