From 2bf2d6f1d2bd3f55f4f177942d3ea7a3592643fe Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Mon, 16 Dec 2013 00:01:50 +0100 Subject: [PATCH] Show better error when json fail to parse template Change-Id: I0d888fc180dd118c50b93ce4393f06d89780a0c5 Partial-Bug: 1261224 --- heatclient/tests/test_shell.py | 17 ++++++++++++++++- heatclient/v1/shell.py | 12 ++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/heatclient/tests/test_shell.py b/heatclient/tests/test_shell.py index 2401f0cb..ac97e797 100644 --- a/heatclient/tests/test_shell.py +++ b/heatclient/tests/test_shell.py @@ -11,7 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import httplib2 import os import re import six @@ -19,6 +18,7 @@ import sys import yaml import fixtures +import httplib2 import tempfile import testscenarios import testtools @@ -904,6 +904,21 @@ class ShellTestStandaloneToken(ShellTestUserPass): # The StanaloneMode shouldn't need any keystoneclient stubbing pass + def test_bad_template_file(self): + failed_msg = 'Cannot parse template file:' + + with tempfile.NamedTemporaryFile() as bad_json_file: + bad_json_file.write("{foo:}") + bad_json_file.flush() + self.shell_error("stack-create ts -f %s" % bad_json_file.name, + failed_msg) + + with tempfile.NamedTemporaryFile() as bad_json_file: + bad_json_file.write('{"foo": None}') + bad_json_file.flush() + self.shell_error("stack-create ts -f %s" % bad_json_file.name, + failed_msg) + class ShellEnvironmentTest(TestCase): diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index 9111bea8..35443dcd 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -28,7 +28,11 @@ def _set_template_fields(hc, args, fields): if args.template_file: tpl = open(args.template_file).read() if tpl.startswith('{'): - fields['template'] = jsonutils.loads(tpl) + try: + fields['template'] = jsonutils.loads(tpl) + except ValueError as e: + raise exc.CommandError( + "Cannot parse template file: %s" % e) else: fields['template'] = tpl elif args.template_url: @@ -36,7 +40,11 @@ def _set_template_fields(hc, args, fields): elif args.template_object: template_body = hc.http_client.raw_request('GET', args.template_object) if template_body: - fields['template'] = jsonutils.loads(template_body) + try: + fields['template'] = jsonutils.loads(template_body) + except ValueError as e: + raise exc.CommandError( + "Cannot parse template file: %s" % e) else: raise exc.CommandError('Could not fetch template from %s' % args.template_object)