From 62e173ef863d03234d05e748368446dcdf3f3daa Mon Sep 17 00:00:00 2001 From: Jianing YANG Date: Tue, 25 Jun 2013 23:55:40 +0800 Subject: [PATCH] Handle YAML parser error as well Fixes bug: #1185590 Change-Id: Id561e2769d1f0192dd831450f10e05e8213bffa8 --- heat/common/template_format.py | 2 +- heat/tests/test_parser.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/heat/common/template_format.py b/heat/common/template_format.py index 0cdae374..ac897e02 100644 --- a/heat/common/template_format.py +++ b/heat/common/template_format.py @@ -40,7 +40,7 @@ def parse(tmpl_str, add_template_sections=True): else: try: tpl = yaml.safe_load(tmpl_str) - except yaml.scanner.ScannerError as e: + except (yaml.scanner.ScannerError, yaml.parser.ParserError) as e: raise ValueError(e) else: if tpl is None: diff --git a/heat/tests/test_parser.py b/heat/tests/test_parser.py index 13788fe2..ed008dc7 100644 --- a/heat/tests/test_parser.py +++ b/heat/tests/test_parser.py @@ -135,6 +135,22 @@ class TemplateTest(HeatTestCase): self.assertEqual(empty[template.RESOURCES], {}) self.assertEqual(empty[template.OUTPUTS], {}) + def test_invalid_template(self): + scanner_error = ''' +1 +Mappings: + ValidMapping: + TestKey: TestValue +''' + parser_error = ''' +Mappings: + ValidMapping: + TestKey: {TestKey1: "Value1" TestKey2: "Value2"} +''' + + self.assertRaises(ValueError, template_format.parse, scanner_error) + self.assertRaises(ValueError, template_format.parse, parser_error) + def test_invalid_section(self): tmpl = parser.Template({'Foo': ['Bar']}) try: -- 2.45.2