From 14982ff0b6763f1a0506a3b1d3422435276d3f7f Mon Sep 17 00:00:00 2001 From: Xiaoxue Wang Date: Fri, 20 Dec 2024 11:25:20 +0800 Subject: [PATCH] move the empty conent check to func start Signed-off-by: Xiaoxue Wang --- insights/core/__init__.py | 18 ++++++++---------- insights/tests/test_json_parser.py | 2 -- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/insights/core/__init__.py b/insights/core/__init__.py index ce7f2e8a65..673248cf9d 100644 --- a/insights/core/__init__.py +++ b/insights/core/__init__.py @@ -780,6 +780,9 @@ class JSONParser(Parser, LegacyItemAccess): A parser class that reads JSON files. Base your own parser on this. """ def parse_content(self, content): + # If content is empty then raise a skip exception instead of a parse exception. + if not content: + raise SkipComponent("Empty output.") try: if isinstance(content, list): # Find the actual json start line with '{' and '[' as identifier @@ -790,20 +793,15 @@ def parse_content(self, content): if line and line.startswith('{') or line.startswith('['): actual_start_index = idx break - print("content[actual_start_index:]: ", content[actual_start_index:]) self.data = json.loads('\n'.join(content[actual_start_index:])) else: self.data = json.loads(content) except: - # If content is empty then raise a skip exception instead of a parse exception. - if not content: - raise SkipComponent("Empty output.") - else: - tb = sys.exc_info()[2] - cls = self.__class__ - name = ".".join([cls.__module__, cls.__name__]) - msg = "%s couldn't parse json." % name - six.reraise(ParseException, ParseException(msg), tb) + tb = sys.exc_info()[2] + cls = self.__class__ + name = ".".join([cls.__module__, cls.__name__]) + msg = "%s couldn't parse json." % name + six.reraise(ParseException, ParseException(msg), tb) # Kept for backwards compatibility; # JSONParser used to raise an exception for valid "null" JSON string if self.data is None: diff --git a/insights/tests/test_json_parser.py b/insights/tests/test_json_parser.py index 4b17a0249c..4e25d32462 100644 --- a/insights/tests/test_json_parser.py +++ b/insights/tests/test_json_parser.py @@ -62,8 +62,6 @@ def test_json_parser_with_extra_header_lines(): ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_2) assert MyJsonParser(ctx).data == {} - # with pytest.raises(SkipComponent) as ex: - # MyJsonParser(ctx) ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_3) with pytest.raises(ParseException) as ex: