From 05128a76fd24c8a1495a80e09d02e7550dc4c016 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 14 Mar 2019 12:05:29 -0700 Subject: [PATCH] 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. Related: rhbz#1689314 --- src/pylorax/ltmpl.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/pylorax/ltmpl.py b/src/pylorax/ltmpl.py index bc30e4af..b011bb3d 100644 --- a/src/pylorax/ltmpl.py +++ b/src/pylorax/ltmpl.py @@ -69,11 +69,17 @@ class LoraxTemplate(object): # mako template now returns unicode strings lines = map(lambda line: line.encode("utf8"), lines) - # split with shlex and perform brace expansion - lines = map(split_and_expand, lines) - - self.lines = 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 + self.lines = expanded_lines + return expanded_lines def split_and_expand(line): return [exp for word in shlex.split(line) for exp in brace_expand(word)]