Skip to content

Commit 5daee4a

Browse files
authored
Merge pull request #389 from splunk/give_filename_when_yml_parsing_errors_out
Improve YML parsing error output
2 parents 8f322aa + 42fd4e7 commit 5daee4a

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

contentctl/input/yml_reader.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Dict, Any
2-
import yaml
3-
import sys
41
import pathlib
2+
import sys
3+
from typing import Any, Dict
4+
5+
import yaml
56

67

78
class YmlReader:
@@ -13,40 +14,44 @@ def load_file(
1314
) -> Dict[str, Any]:
1415
try:
1516
file_handler = open(file_path, "r", encoding="utf-8")
17+
except OSError as exc:
18+
print(
19+
f"\nThere was an unrecoverable error when opening the file '{file_path}' - we will exit immediately:\n{str(exc)}"
20+
)
21+
sys.exit(1)
1622

1723
# The following code can help diagnose issues with duplicate keys or
1824
# poorly-formatted but still "compliant" YML. This code should be
1925
# enabled manually for debugging purposes. As such, strictyaml
2026
# library is intentionally excluded from the contentctl requirements
2127

28+
try:
2229
if STRICT_YML_CHECKING:
30+
# This is an extra level of verbose parsing that can be
31+
# enabled for debugging purpose. It is intentionally done in
32+
# addition to the regular yml parsing
2333
import strictyaml
2434

25-
try:
26-
strictyaml.dirty_load(file_handler.read(), allow_flow_style=True)
27-
file_handler.seek(0)
28-
except Exception as e:
29-
print(f"Error loading YML file {file_path}: {str(e)}")
30-
sys.exit(1)
31-
try:
32-
# Ideally we should use
33-
# from contentctl.actions.new_content import NewContent
34-
# and use NewContent.UPDATE_PREFIX,
35-
# but there is a circular dependency right now which makes that difficult.
36-
# We have instead hardcoded UPDATE_PREFIX
37-
UPDATE_PREFIX = "__UPDATE__"
38-
data = file_handler.read()
39-
if UPDATE_PREFIX in data:
40-
raise Exception(
41-
f"The file {file_path} contains the value '{UPDATE_PREFIX}'. Please fill out any unpopulated fields as required."
42-
)
43-
yml_obj = yaml.load(data, Loader=yaml.CSafeLoader)
44-
except yaml.YAMLError as exc:
45-
print(exc)
46-
sys.exit(1)
47-
48-
except OSError as exc:
49-
print(exc)
35+
strictyaml.dirty_load(file_handler.read(), allow_flow_style=True)
36+
file_handler.seek(0)
37+
38+
# Ideally we should use
39+
# from contentctl.actions.new_content import NewContent
40+
# and use NewContent.UPDATE_PREFIX,
41+
# but there is a circular dependency right now which makes that difficult.
42+
# We have instead hardcoded UPDATE_PREFIX
43+
UPDATE_PREFIX = "__UPDATE__"
44+
data = file_handler.read()
45+
if UPDATE_PREFIX in data:
46+
raise Exception(
47+
f"\nThe file {file_path} contains the value '{UPDATE_PREFIX}'. Please fill out any unpopulated fields as required."
48+
)
49+
yml_obj = yaml.load(data, Loader=yaml.CSafeLoader)
50+
51+
except yaml.YAMLError as exc:
52+
print(
53+
f"\nThere was an unrecoverable YML Parsing error when reading or parsing the file '{file_path}' - we will exit immediately:\n{str(exc)}"
54+
)
5055
sys.exit(1)
5156

5257
if add_fields is False:

0 commit comments

Comments
 (0)