From 514f0650b2a857d6516d6ceb69aba6335d5618f2 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 16 Oct 2015 14:47:04 -0400 Subject: [PATCH] Give the user a better error message (without a stack trace) when there is a yaml error. Signed-off-by: Daniel Nephin --- compose/config/config.py | 5 +++-- tests/unit/config/config_test.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/compose/config/config.py b/compose/config/config.py index 3bcd769ad5..59b98f6092 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -610,5 +610,6 @@ def load_yaml(filename): try: with open(filename, 'r') as fh: return yaml.safe_load(fh) - except IOError as e: - raise ConfigurationError(six.text_type(e)) + except (IOError, yaml.YAMLError) as e: + error_name = getattr(e, '__module__', '') + '.' + e.__class__.__name__ + raise ConfigurationError(u"{}: {}".format(error_name, e)) diff --git a/tests/unit/config/config_test.py b/tests/unit/config/config_test.py index 20ae7fa305..b4bd9c7123 100644 --- a/tests/unit/config/config_test.py +++ b/tests/unit/config/config_test.py @@ -5,6 +5,7 @@ import shutil import tempfile from operator import itemgetter +import py import pytest from compose.config import config @@ -349,6 +350,18 @@ class ConfigTest(unittest.TestCase): ) ) + def test_load_yaml_with_yaml_error(self): + tmpdir = py.test.ensuretemp('invalid_yaml_test') + invalid_yaml_file = tmpdir.join('docker-compose.yml') + invalid_yaml_file.write(""" + web: + this is bogus: ok: what + """) + with pytest.raises(ConfigurationError) as exc: + config.load_yaml(str(invalid_yaml_file)) + + assert 'line 3, column 32' in exc.exconly() + class InterpolationTest(unittest.TestCase): @mock.patch.dict(os.environ)