From 9b6ae242df58445a1df740e4835a18f46f8b1f23 Mon Sep 17 00:00:00 2001 From: Michael Gummelt Date: Mon, 10 Aug 2015 12:27:56 -0700 Subject: [PATCH] Catch exceptions when parsing config file --- cli/tests/data/config/parse_error.toml | 2 ++ cli/tests/integrations/test_config.py | 13 +++++++++++++ dcos/config.py | 7 ++++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 cli/tests/data/config/parse_error.toml diff --git a/cli/tests/data/config/parse_error.toml b/cli/tests/data/config/parse_error.toml new file mode 100644 index 0000000..1809800 --- /dev/null +++ b/cli/tests/data/config/parse_error.toml @@ -0,0 +1,2 @@ +header] +key=value diff --git a/cli/tests/integrations/test_config.py b/cli/tests/integrations/test_config.py index c170cc6..b365833 100644 --- a/cli/tests/integrations/test_config.py +++ b/cli/tests/integrations/test_config.py @@ -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) diff --git a/dcos/config.py b/dcos/config.py index d93b9d0..3d0402a 100644 --- a/dcos/config.py +++ b/dcos/config.py @@ -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)