You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ch...@apache.org on 2020/08/04 15:15:15 UTC

[qpid-dispatch] branch master updated: DISPATCH-1737: Log config file json as-modified when json parse fails

This is an automated email from the ASF dual-hosted git repository.

chug pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git


The following commit(s) were added to refs/heads/master by this push:
     new 578164f  DISPATCH-1737: Log config file json as-modified when json parse fails
578164f is described below

commit 578164f5ed4b84eeb31c71148cb3ba4843451248
Author: Chuck Rolke <ch...@apache.org>
AuthorDate: Tue Aug 4 11:13:55 2020 -0400

    DISPATCH-1737: Log config file json as-modified when json parse fails
    
    When config file json parsing fails then the parser prints a line and
    column number of the offending input. Since the original input is modified
    by the config process the reported line and column numbers are not useful.
    
    This patch logs the input that json was given when parsing fails.
    The parser failure line and column numbers directly relate to logged
    json input lines and syntax errors may be identifed with some authority.
    
    This closes #808
---
 python/qpid_dispatch_internal/management/config.py | 29 ++++++++++++++++++----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/python/qpid_dispatch_internal/management/config.py b/python/qpid_dispatch_internal/management/config.py
index 6136cea..031d41c 100644
--- a/python/qpid_dispatch_internal/management/config.py
+++ b/python/qpid_dispatch_internal/management/config.py
@@ -40,7 +40,7 @@ from qpid_dispatch_internal.compat import PY_STRING_TYPE
 from qpid_dispatch_internal.compat import PY_TEXT_TYPE
 
 try:
-    from ..dispatch import LogAdapter, LOG_WARNING
+    from ..dispatch import LogAdapter, LOG_WARNING, LOG_ERROR
     _log_imported = True
 except ImportError:
     # unit test cannot import since LogAdapter not set up
@@ -190,19 +190,26 @@ class Config(object):
         spare_comma = re.compile(r',\s*([]}])') # Strip spare commas
         js_text = re.sub(spare_comma, r'\1', js_text)
         # Convert dictionary keys to camelCase
-        sections = json.loads(js_text)
+        try:
+            sections = json.loads(js_text)
+        except Exception as e:
+            self.dump_json("Contents of failed config file", js_text)
+            raise
         Config.transform_sections(sections)
         return sections
 
-    @staticmethod
-    def _parserawjson(lines):
+    def _parserawjson(self, lines):
         """Parse raw json config file format into a section list"""
         def sub(line):
             # ignore comment lines that start with "[whitespace] #"
             line = "" if line.strip().startswith('#') else line
             return line
         js_text = "%s"%("\n".join([sub(l) for l in lines]))
-        sections = json.loads(js_text)
+        try:
+            sections = json.loads(js_text)
+        except Exception as e:
+            self.dump_json("Contents of failed json-format config file", js_text)
+            raise
         Config.transform_sections(sections)
         return sections
 
@@ -237,6 +244,18 @@ class Config(object):
     def remove(self, entity):
         self.entities.remove(entity)
 
+    def dump_json(self, title, js_text):
+        # Function for config file parse failure logging.
+        # js_text is the pre-processed config-format json string or the
+        # raw json-format string that was presented to the json interpreter.
+        # The logs generated here correlate exactly to the line, column,
+        # and character numbers reported by json error exceptions.
+        # For each line 'Column 1' immediately follows the vertical bar.
+        self._log(LOG_ERROR, title)
+        lines = js_text.split("\n")
+        for idx in range(len(lines)):
+            self._log(LOG_ERROR, "Line %d |%s" % (idx + 1, lines[idx]))
+
 class PolicyConfig(Config):
     def __init__(self, filename=None, schema=QdSchema(), raw_json=False):
         super(PolicyConfig, self).__init__(filename, schema, raw_json)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org