Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'polite' YAML syntax validation and hinting #799

Merged
merged 1 commit into from Aug 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ansible/errors.py
Expand Up @@ -30,3 +30,5 @@ class AnsibleFileNotFound(AnsibleError):
class AnsibleConnectionFailed(AnsibleError):
pass

class AnsibleYAMLValidationFailed(AnsibleError):
pass
22 changes: 21 additions & 1 deletion lib/ansible/utils.py
Expand Up @@ -229,9 +229,29 @@ def parse_yaml_from_file(path):

try:
data = file(path).read()
return parse_yaml(data)
except IOError:
raise errors.AnsibleError("file not found: %s" % path)
return parse_yaml(data)
except yaml.YAMLError, exc:
if hasattr(exc, 'problem_mark'):
mark = exc.problem_mark
if mark.line -1 >= 0:
before_probline = data.split("\n")[mark.line-1]
else:
before_probline = ''
probline = data.split("\n")[mark.line]
arrow = " " * mark.column + "^"
msg = """Syntax Error while loading YAML script, %s
Note: The error may actually appear before this position: line %s, column %s

%s
%s
%s""" % (path, mark.line + 1, mark.column + 1, before_probline, probline, arrow)
else:
# No problem markers means we have to throw a generic
# "stuff messed up" type message. Sry bud.
msg = "Could not parse YAML. Check over %s again." % path
raise errors.AnsibleYAMLValidationFailed(msg)

def parse_kv(args):
''' convert a string of key/value items to a dict '''
Expand Down