better error messages

This commit is contained in:
Tim Miller 2013-01-29 21:22:17 -08:00
parent d211c85788
commit 6c04767006
3 changed files with 10 additions and 8 deletions

View File

@ -33,17 +33,19 @@ def render_template(template, config):
if is_executable(template):
return render_executable(template, config)
else:
return render_moustache(open(template).read(), config)
try:
return render_moustache(open(template).read(), config)
except KeyNotFoundError as e:
raise CornfigException("key '%s' does not exist in metadata file '%s'" % (e.key, template))
except Exception as e:
raise CornfigException("could not render moustache template %s" % template)
def is_executable(path):
return os.path.isfile(path) and os.access(path, os.X_OK)
def render_moustache(text, config):
try:
r = pystache.Renderer(missing_tags = 'strict')
return r.render(text, config)
except KeyNotFoundError as e:
raise CornfigException("key '%s' does not exist in metadata file." % e.key)
r = pystache.Renderer(missing_tags = 'strict')
return r.render(text, config)
def render_executable(path, config):
p = Popen([path], stdin=PIPE, stdout=PIPE, stderr=PIPE)

View File

@ -9,7 +9,7 @@ config = {
'author': 'echohead',
'author_email': 'tim.miller.0@gmail.com',
'url': 'http://github.com/echohead/cornfig',
'version': '0.1',
'version': '0.2',
'install_requires': ['nose'],
'packages': ['cornfig'],
'scripts': [],

View File

@ -57,7 +57,7 @@ def test_render_template():
def test_render_moustache():
assert_equals( render_moustache("ab{{x.a}}cd", {"x": {"a": "123"}}), "ab123cd" )
@raises(CornfigException)
@raises(Exception)
def test_render_moustache_bad_key():
render_moustache("{{badkey}}", {})