Skip to content

Commit

Permalink
move the empty conent check to func start
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxue Wang <[email protected]>
  • Loading branch information
JoySnow committed Dec 20, 2024
1 parent d5c9c33 commit 14982ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 8 additions & 10 deletions insights/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions insights/tests/test_json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 14982ff

Please sign in to comment.