Improve logging for template syntax errors

The shlex splitting can fail, resulting in error messages like:

ERROR livemedia-creator: No closing quotation

without any context in the log files. This logs the line that failed to
be split and expanded.

(cherry picked from commit f9665940bb)
(cherry picked from commit 4c0e632b93)
This commit is contained in:
Brian C. Lane 2019-03-14 12:05:29 -07:00
parent 6aa0a3fdf6
commit 78ccea2231
1 changed files with 10 additions and 4 deletions

View File

@ -67,10 +67,16 @@ class LoraxTemplate(object):
# remove comments
lines = [line for line in lines if not line.startswith("#")]
# split with shlex and perform brace expansion
lines = [split_and_expand(line) for line in lines]
return lines
# split with shlex and perform brace expansion. This can fail, so we unroll the loop
# for better error reporting.
expanded_lines = []
try:
for line in lines:
expanded_lines.append(split_and_expand(line))
except Exception as e:
logger.error('shlex error processing "%s": %s', line, str(e))
raise
return expanded_lines
def split_and_expand(line):
return [exp for word in shlex.split(line) for exp in brace_expand(word)]